Commit 8574a2e
committed
drivers/libusb1.c: use explicit context; drop per-close libusb_exit() [#598]
The original code called libusb_init(NULL) on every nut_libusb_open() and
libusb_exit(NULL) on every nut_libusb_close(), bumping and decrementing
the default context's refcount. That violates the documented contract for
libusb_exit, which "should be called after closing all open devices and
before your application terminates", and on certain firmware it wedges
indefinitely: when libusb_exit hits the 1->0 transition it tears the
context down, which waits on libusb's internal sync primitives for
outstanding URBs to drain. URBs orphaned by libusb_reset_device or by an
unexpected device disconnect never drain, so the wait never returns.
The deadlock is reachable from any reconnect path. It was first
recognized by 4f84b7f ("don't libusb_exit() when closing a previously
opened device"), which dropped the call in nut_libusb_open()'s rematch
loop. The fallthrough reconnect path in qx_command and the rest of the
driver lifecycle still hit it. With the companion change in nutdrv_qx.c
(a4813f868) that escalates persistent LIBUSB_ERROR_OVERFLOW to
libusb_reset_device, the deadlock window opens on every escalation and
was hit reproducibly on the 0665:5161 Cypress USB-serial bridge family
(Salicru SPS, Ippon, ViewPower, Voltronic Power UPSes; see #598, #993,
#2453).
Switch to an explicit libusb context owned end-to-end:
- Initialize once on the first nut_libusb_open() and register a
matching one-shot atexit handler.
- Remove libusb_exit() from nut_libusb_close(); the close path now
only calls libusb_close(), which is non-blocking.
- Replace libusb_get_device_list(NULL, ...) with the explicit context
so the default context is never touched.
- Bound the atexit libusb_exit() with a SIGALRM-based 2s timeout
(POSIX) so a deadlock-prone teardown at shutdown can't wedge
supervisor stop sequences (e.g. systemctl stop). Sub-millisecond on
a healthy context.
Bump USB_DRIVER_VERSION from 0.53 to 0.54.
Validated on a Salicru SPS 1500 ONE BL (0665:5161) by authorize-toggle
stress reproduction (8 cycles, clean reconnect each time, same driver
PID throughout) and by a natural OVERFLOW event captured in soak
(counter-gated logic at 1/3, next poll succeeded, no escalation; driver
PID stable for 22h+).
Signed-off-by: Pedro Cunha <pedroagracio+nut@gmail.com>1 parent 0d2c7a1 commit 8574a2e
1 file changed
Lines changed: 95 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
43 | 48 | | |
44 | 49 | | |
45 | | - | |
| 50 | + | |
46 | 51 | | |
47 | 52 | | |
48 | 53 | | |
| |||
55 | 60 | | |
56 | 61 | | |
57 | 62 | | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
58 | 133 | | |
59 | 134 | | |
60 | 135 | | |
| |||
291 | 366 | | |
292 | 367 | | |
293 | 368 | | |
294 | | - | |
295 | | - | |
296 | | - | |
297 | | - | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
298 | 381 | | |
299 | 382 | | |
300 | 383 | | |
| |||
312 | 395 | | |
313 | 396 | | |
314 | 397 | | |
315 | | - | |
| 398 | + | |
316 | 399 | | |
317 | 400 | | |
318 | 401 | | |
| |||
1166 | 1249 | | |
1167 | 1250 | | |
1168 | 1251 | | |
1169 | | - | |
| 1252 | + | |
| 1253 | + | |
| 1254 | + | |
| 1255 | + | |
| 1256 | + | |
| 1257 | + | |
1170 | 1258 | | |
1171 | 1259 | | |
1172 | 1260 | | |
| |||
0 commit comments