Commit 5dee961
authored
Fix config entry FAILED_UNLOAD on hung BLE disconnect, add disconnect tracing (#359)
`BleakClient.disconnect()` can block until the connect timeout (default 20s) when a write-with-response is still pending on the transport after a mid-auth BLE drop, which is common through an ESPHome proxy. `Connection.disconnect()` awaited it with no deadline, so `async_unload_entry` could outrun HA's unload window and leave the entry in `ConfigEntryState.FAILED_UNLOAD` - which only a full restart recovers from, re-triggering the same flap.
Every client teardown now goes through a bounded `_disconnect_client()` helper (`asyncio.timeout`, `DISCONNECT_TIMEOUT = 5s`) that swallows the usual "already down" errors and, on timeout, gives up waiting and lets the caller finish local cleanup - the transport drains on its own once the drop is detected. This enforces the deadline in the library where the hang actually happens, so all five teardown paths benefit and no `async_unload_entry` guard is needed.
While here, make disconnects traceable from a diagnostics download alone (no debug logging required), since the original report was hard to pin down:
- `disconnect()` and the bleak `disconnected()` callback record the caller chain as the `reason` on the `DISCONNECTING` / `DISCONNECTED` entries in `connection_state_history`, so a requested disconnect (unload, reload) is distinguishable from an unsolicited bleak drop.
- `_disconnect_client()` records each outcome (`ok` / `timeout` / `already_down`) with its trigger into a bounded `disconnect_log` that is included in the diagnostics dump, so timeouts and which path caused them are visible across every teardown path.
Resolves #338. Reimplements #339 with the fix moved into the library and the root cause framed correctly.
Should also address #3661 parent 2828ce8 commit 5dee961
4 files changed
Lines changed: 95 additions & 29 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
192 | 192 | | |
193 | 193 | | |
194 | 194 | | |
195 | | - | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
196 | 199 | | |
197 | 200 | | |
198 | 201 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | | - | |
| 14 | + | |
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| |||
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
46 | | - | |
| 46 | + | |
47 | 47 | | |
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
53 | 59 | | |
54 | 60 | | |
55 | 61 | | |
| |||
236 | 242 | | |
237 | 243 | | |
238 | 244 | | |
| 245 | + | |
239 | 246 | | |
240 | 247 | | |
241 | 248 | | |
| |||
257 | 264 | | |
258 | 265 | | |
259 | 266 | | |
| 267 | + | |
260 | 268 | | |
261 | 269 | | |
262 | 270 | | |
| |||
387 | 395 | | |
388 | 396 | | |
389 | 397 | | |
390 | | - | |
391 | | - | |
| 398 | + | |
392 | 399 | | |
393 | 400 | | |
394 | 401 | | |
| |||
405 | 412 | | |
406 | 413 | | |
407 | 414 | | |
408 | | - | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
409 | 419 | | |
410 | 420 | | |
411 | 421 | | |
| |||
422 | 432 | | |
423 | 433 | | |
424 | 434 | | |
425 | | - | |
| 435 | + | |
426 | 436 | | |
427 | 437 | | |
428 | 438 | | |
| |||
476 | 486 | | |
477 | 487 | | |
478 | 488 | | |
479 | | - | |
480 | | - | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
481 | 529 | | |
482 | 530 | | |
483 | 531 | | |
484 | 532 | | |
485 | 533 | | |
486 | 534 | | |
487 | | - | |
488 | | - | |
489 | | - | |
490 | | - | |
491 | | - | |
| 535 | + | |
| 536 | + | |
492 | 537 | | |
493 | 538 | | |
494 | 539 | | |
495 | | - | |
| 540 | + | |
496 | 541 | | |
497 | 542 | | |
498 | 543 | | |
499 | | - | |
500 | | - | |
501 | | - | |
502 | | - | |
503 | | - | |
| 544 | + | |
504 | 545 | | |
505 | 546 | | |
506 | 547 | | |
| |||
601 | 642 | | |
602 | 643 | | |
603 | 644 | | |
604 | | - | |
605 | | - | |
606 | | - | |
607 | | - | |
| 645 | + | |
608 | 646 | | |
609 | 647 | | |
610 | 648 | | |
| |||
621 | 659 | | |
622 | 660 | | |
623 | 661 | | |
| 662 | + | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
624 | 666 | | |
625 | | - | |
| 667 | + | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
626 | 671 | | |
627 | 672 | | |
628 | 673 | | |
629 | 674 | | |
630 | 675 | | |
| 676 | + | |
631 | 677 | | |
632 | 678 | | |
633 | 679 | | |
| |||
1041 | 1087 | | |
1042 | 1088 | | |
1043 | 1089 | | |
1044 | | - | |
1045 | | - | |
| 1090 | + | |
1046 | 1091 | | |
1047 | 1092 | | |
1048 | 1093 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| 29 | + | |
29 | 30 | | |
30 | 31 | | |
31 | 32 | | |
| |||
291 | 292 | | |
292 | 293 | | |
293 | 294 | | |
294 | | - | |
| 295 | + | |
295 | 296 | | |
296 | 297 | | |
297 | 298 | | |
| |||
300 | 301 | | |
301 | 302 | | |
302 | 303 | | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
303 | 308 | | |
304 | 309 | | |
305 | 310 | | |
306 | 311 | | |
307 | 312 | | |
308 | | - | |
| 313 | + | |
309 | 314 | | |
310 | 315 | | |
311 | 316 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
352 | 352 | | |
353 | 353 | | |
354 | 354 | | |
| 355 | + | |
| 356 | + | |
355 | 357 | | |
356 | 358 | | |
357 | 359 | | |
| |||
592 | 594 | | |
593 | 595 | | |
594 | 596 | | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
0 commit comments