Commit 19ca812
fix: clean up the async conversion's output file when cancelled
Offloading the JSON-to-NDJSON conversion to a thread last commit removed the event
loop block but introduced a leak: a thread cannot be cancelled, so on cancellation
the conversion still runs to completion and creates its file, while the awaiting
coroutine has already raised CancelledError and discarded the path. Nothing was left
that could delete it. Reproduced -- cancel mid-conversion and an
unst_elements_*.ndjson survives.
The conversion is now awaited through asyncio.shield, which keeps a handle on the
thread's result after the caller stops waiting, and a done callback unlinks the
finished file. A conversion that raised needs no callback, since it already removes
its own partial file.
This is the same shape as the split hook's cancellation race: work that outlives the
operation that requested it, publishing a path nobody will read. It did not exist on
the inline version, which had no await point to cancel at.
Regression test asserts against the path production actually created, so it cannot
pass by observing that no file was ever made. Confirmed to fail against the plain
awaited to_thread.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>1 parent 140ced0 commit 19ca812
2 files changed
Lines changed: 68 additions & 3 deletions
File tree
- _test_unstructured_client/unit
- src/unstructured_client
Lines changed: 40 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
16 | 17 | | |
| 18 | + | |
17 | 19 | | |
18 | 20 | | |
19 | 21 | | |
| |||
752 | 754 | | |
753 | 755 | | |
754 | 756 | | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
| 784 | + | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
83 | 83 | | |
84 | 84 | | |
85 | 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 | + | |
86 | 113 | | |
87 | 114 | | |
88 | 115 | | |
| |||
383 | 410 | | |
384 | 411 | | |
385 | 412 | | |
386 | | - | |
387 | | - | |
388 | | - | |
| 413 | + | |
389 | 414 | | |
390 | 415 | | |
391 | 416 | | |
| |||
0 commit comments