Repository navigation
fix: write the last batch when WriteApi is closed in an atexit handler - #733
Open
sankalpsthakur wants to merge 1 commit into
Open
sankalpsthakur wants to merge 1 commit into
sankalpsthakur wants to merge 1 commit into
Conversation
Since Python 3.9 concurrent.futures shuts its thread pools down before atexit handlers run, so the batching pipeline could no longer schedule the final write and dropped the batch with "cannot schedule new futures after interpreter shutdown". When the write scheduler refuses the batch because the interpreter is shutting down, write it on the current thread instead of dropping it. Fixes influxdata#644
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #733 +/- ##
==========================================
- Coverage 90.49% 90.47% -0.03%
==========================================
Files 40 40
Lines 3568 3570 +2
==========================================
+ Hits 3229 3230 +1
- Misses 339 340 +1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #644
Proposed Changes
Since Python 3.9,
concurrent.futuresshuts its thread pools down beforeatexithandlers run. The batchingWriteApischedules each write on aThreadPoolScheduler, so from that point on the scheduler refuses the batch withRuntimeError: cannot schedule new futures after interpreter shutdown, and the batch is logged as failed and dropped. Closing theWriteApifrom anatexithandler, as the README andexamples/iot_sensor.pyshow, doesn't help: theflush_intervaltimer usually fires while the thread pools are shutting down, which is why the error in the issue is printed beforeclosing.This catches that error after the scheduling steps in
_to_responseand writes the batch on the current thread instead. Any other error is handled as before.Behavior notes:
WriteApinow also gets its pending batch written instead of dropped. If InfluxDB is unreachable at that point, exit can take up tomax_retry_timewhile the write is retried.atexit, after the application's handlers, so the bug doesn't occur there and this code path isn't reached.ApiClient.__del__. This is a different code path.Checklist
pytest tests/test_WriteApiBatching.pycompletes successfully (30 passed, including the new tests)Tests
BatchingWriteInterpreterShutdownTestintests/test_WriteApiBatching.pyruns the scenario from the issue in a separate interpreter against a local HTTP server:test_close_in_atexit: a batch written before exit arrives when theWriteApiis closed in anatexithandler.test_write_and_close_in_atexit: a point written inside theatexithandler itself arrives.Both fail on master with the error from the issue and pass with this change (Python 3.11 and 3.13; reactivex 4.0.4, 5.1.0 and 6.1.0).
AI/LLM disclosure