Commit ae12935
authored
fix(scrapy): async-thread startup race, shutdown lifecycle, and timeout setting (#979)
## Description
Fixes several defects in the Scrapy integration's background event-loop
thread (`AsyncThread`), the scheduler, and the HTTP cache storage, and
makes the loop timeout configurable.
## Fixes
- **`run_coro` startup race** — the `is_running()` guard fired
spuriously when a coroutine was submitted before the loop thread reached
`run_forever()` (observed ~122/500 in `scheduler.open()`). It now guards
on `is_closed()`. A coroutine queued on a not-yet-running loop runs once
the loop starts; only a closed loop raises.
- **`close()` thread leak** — if task cancellation timed out or raised,
the loop was never stopped or joined. Stop, join, and the
forced-shutdown fallback now run in a `finally`, and the original error
still propagates.
- **`close()` second call** — a repeated close raised `RuntimeError:
Event loop is closed`. An `is_closed()` early-return makes it a no-op.
- **`close()` ignored its `timeout`** for the cancellation step (it used
the constructor default). It now passes the caller's timeout through.
- **`run_coro` timeout** left the coroutine running. It now cancels the
future on timeout.
- **HTTP cache open/cleanup thread leaks** — `open_spider` now closes
the thread if opening the key-value store fails (matching
`ApifyScheduler.open`). The expiration sweep runs inside `try` with
`close()` in a `finally`.
- **Configurable timeout (#955)** — new
`APIFY_ASYNC_THREAD_TIMEOUT_SECS` setting, wired into the scheduler (via
`from_crawler`) and the cache storage.
## Error logging
The integration now follows consistent conventions for caught
exceptions:
- **`except … as exc:` → `logger.warning(f'… {exc}')`, swallowed** — for
*expected, recoverable* conditions handled locally: a malformed or
legacy stored payload skipped as a cache/queue miss, or non-UTF-8
headers preserved in the serialized request. A short message plus the
exception text, with no traceback, because it is not a bug.
- **`except Exception:` → `logger.exception('…')`, swallowed** — for
*unexpected* failures handled at a terminal point: the cleanup sweep,
shutdown, or skip-and-continue. `logger.exception` attaches the full
traceback, and nothing re-raises because the error is handled here.
- **`except …:` → `raise` (no logging)** — when the error is re-raised
and the caller or Scrapy logs it with a traceback anyway. `run_coro`'s
timeout path cancels the future and re-raises without logging, so the
failure is reported once.
- **`except Exception:` → `logger.exception('…'); raise`** — the
boundary log, used only where local context materially helps *and* the
propagated error would otherwise be logged only generically or not at
all. The scheduler's `next_request` / `enqueue_request` /
`has_pending_requests` are called synchronously by the Scrapy engine
(not inside a Deferred), so without this log the Apify-specific context
would be lost.
**Why `logger.exception` replaced `traceback.print_exc()`:**
`traceback.print_exc()` writes a bare traceback straight to stderr,
bypassing logging entirely. It has no level, no logger name, no message,
and ignores Scrapy's and the SDK's log configuration and handlers.
`logger.exception(msg)` logs at ERROR through the configured logging, so
it is routed, formatted, and filterable like every other log line. It
adds a message explaining *what* failed and still attaches the full
traceback automatically, which makes including the exception object in
the message (`{exc}`) redundant (ruff TRY401).
## Tests
New `tests/unit/scrapy/test_async_thread.py` covers the startup race,
run-after-close, timeout cancellation, idempotent close, the caller
timeout reaching the shutdown step, and stop/join when task cancellation
fails. The scheduler and HTTP cache test modules gain coverage for the
timeout setting, closing the thread on open failure, and the
cleanup-failure path still closing the thread.1 parent 62eb505 commit ae12935
7 files changed
Lines changed: 487 additions & 84 deletions
File tree
- src/apify/scrapy
- extensions
- tests/unit/scrapy
- extensions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
52 | 52 | | |
53 | 53 | | |
54 | 54 | | |
55 | | - | |
| 55 | + | |
56 | 56 | | |
57 | 57 | | |
58 | 58 | | |
59 | 59 | | |
60 | 60 | | |
61 | 61 | | |
62 | | - | |
63 | | - | |
| 62 | + | |
| 63 | + | |
64 | 64 | | |
65 | 65 | | |
66 | 66 | | |
67 | 67 | | |
68 | 68 | | |
69 | 69 | | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
75 | 75 | | |
76 | 76 | | |
77 | | - | |
| 77 | + | |
78 | 78 | | |
79 | 79 | | |
80 | 80 | | |
81 | 81 | | |
82 | 82 | | |
83 | 83 | | |
84 | | - | |
| 84 | + | |
| 85 | + | |
85 | 86 | | |
86 | | - | |
87 | | - | |
88 | | - | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
89 | 95 | | |
90 | | - | |
91 | | - | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
92 | 104 | | |
93 | | - | |
94 | | - | |
| 105 | + | |
| 106 | + | |
95 | 107 | | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
100 | 112 | | |
101 | 113 | | |
102 | 114 | | |
| |||
125 | 137 | | |
126 | 138 | | |
127 | 139 | | |
128 | | - | |
129 | | - | |
| 140 | + | |
| 141 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
| |||
35 | 36 | | |
36 | 37 | | |
37 | 38 | | |
38 | | - | |
39 | 39 | | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
40 | 45 | | |
| 46 | + | |
| 47 | + | |
41 | 48 | | |
| 49 | + | |
| 50 | + | |
42 | 51 | | |
| 52 | + | |
| 53 | + | |
43 | 54 | | |
| 55 | + | |
| 56 | + | |
44 | 57 | | |
| 58 | + | |
45 | 59 | | |
46 | 60 | | |
47 | | - | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
48 | 69 | | |
49 | 70 | | |
50 | 71 | | |
| |||
62 | 83 | | |
63 | 84 | | |
64 | 85 | | |
65 | | - | |
| 86 | + | |
66 | 87 | | |
67 | | - | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
68 | 101 | | |
69 | 102 | | |
70 | | - | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
71 | 113 | | |
72 | 114 | | |
73 | 115 | | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
74 | 119 | | |
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 | | - | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
106 | 124 | | |
107 | | - | |
108 | | - | |
109 | | - | |
| 125 | + | |
| 126 | + | |
110 | 127 | | |
111 | | - | |
| 128 | + | |
112 | 129 | | |
113 | | - | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
114 | 139 | | |
115 | 140 | | |
116 | | - | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
117 | 155 | | |
118 | 156 | | |
119 | 157 | | |
| |||
122 | 160 | | |
123 | 161 | | |
124 | 162 | | |
125 | | - | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
126 | 170 | | |
127 | 171 | | |
128 | 172 | | |
| |||
139 | 183 | | |
140 | 184 | | |
141 | 185 | | |
| 186 | + | |
142 | 187 | | |
143 | 188 | | |
144 | 189 | | |
| |||
153 | 198 | | |
154 | 199 | | |
155 | 200 | | |
156 | | - | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
157 | 208 | | |
158 | 209 | | |
159 | 210 | | |
| |||
169 | 220 | | |
170 | 221 | | |
171 | 222 | | |
172 | | - | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
173 | 259 | | |
174 | 260 | | |
175 | 261 | | |
| |||
219 | 305 | | |
220 | 306 | | |
221 | 307 | | |
222 | | - | |
| 308 | + | |
| 309 | + | |
223 | 310 | | |
224 | 311 | | |
225 | 312 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
129 | 129 | | |
130 | 130 | | |
131 | 131 | | |
132 | | - | |
133 | | - | |
| 132 | + | |
| 133 | + | |
134 | 134 | | |
135 | 135 | | |
136 | 136 | | |
| |||
0 commit comments