|
39 | 39 | from elastic_transport import OpenTelemetrySpan |
40 | 40 |
|
41 | 41 | from .. import Elasticsearch |
42 | | -from ..compat import to_bytes |
| 42 | +from ..compat import to_bytes, safe_thread |
43 | 43 | from ..exceptions import ApiError, NotFoundError, TransportError |
44 | 44 | from ..serializer import Serializer |
45 | 45 | from .errors import BulkIndexError, ScanError |
@@ -266,35 +266,28 @@ def _chunk_actions( |
266 | 266 | ) |
267 | 267 |
|
268 | 268 | def get_items() -> None: |
269 | | - ret = None |
270 | 269 | try: |
271 | 270 | for item in actions: |
272 | 271 | item_queue.put(item) |
273 | | - except BaseException as exc: |
274 | | - ret = exc |
275 | | - item_queue.put((BulkMeta.done, ret)) |
| 272 | + finally: |
| 273 | + # make sure we signal the end even if there is an exception |
| 274 | + item_queue.put((BulkMeta.done, None)) |
276 | 275 |
|
277 | | - item_getter_job = Thread(target=get_items) |
278 | | - item_getter_job.start() |
279 | | - |
280 | | - timeout: Optional[float] = flush_after_seconds |
281 | | - while True: |
282 | | - try: |
283 | | - action, data = item_queue.get(timeout=timeout) |
284 | | - timeout = flush_after_seconds |
285 | | - except queue.Empty: |
286 | | - action, data = BulkMeta.flush, None |
287 | | - timeout = None |
288 | | - |
289 | | - if action is BulkMeta.done: |
290 | | - if isinstance(data, BaseException): |
291 | | - raise data |
292 | | - break |
293 | | - ret = chunker.feed(action, data) |
294 | | - if ret: |
295 | | - yield ret |
296 | | - |
297 | | - item_getter_job.join() |
| 276 | + with safe_thread(get_items): |
| 277 | + timeout: Optional[float] = flush_after_seconds |
| 278 | + while True: |
| 279 | + try: |
| 280 | + action, data = item_queue.get(timeout=timeout) |
| 281 | + timeout = flush_after_seconds |
| 282 | + except queue.Empty: |
| 283 | + action, data = BulkMeta.flush, None |
| 284 | + timeout = None |
| 285 | + |
| 286 | + if action is BulkMeta.done: |
| 287 | + break |
| 288 | + ret = chunker.feed(action, data) |
| 289 | + if ret: |
| 290 | + yield ret |
298 | 291 |
|
299 | 292 | ret = chunker.flush() |
300 | 293 | if ret: |
|
0 commit comments