Skip to content

fix: write the last batch when WriteApi is closed in an atexit handler - #733

Open
sankalpsthakur wants to merge 1 commit into
influxdata:masterfrom
sankalpsthakur:fix/644-atexit-last-batch
Open

sankalpsthakur wants to merge 1 commit into
influxdata:masterfrom
sankalpsthakur:fix/644-atexit-last-batch

Conversation

@sankalpsthakur

Copy link
Copy Markdown
Contributor

Closes #644

Proposed Changes

Since Python 3.9, concurrent.futures shuts its thread pools down before atexit handlers run. The batching WriteApi schedules each write on a ThreadPoolScheduler, so from that point on the scheduler refuses the batch with RuntimeError: cannot schedule new futures after interpreter shutdown, and the batch is logged as failed and dropped. Closing the WriteApi from an atexit handler, as the README and examples/iot_sensor.py show, doesn't help: the flush_interval timer usually fires while the thread pools are shutting down, which is why the error in the issue is printed before closing.

This catches that error after the scheduling steps in _to_response and writes the batch on the current thread instead. Any other error is handled as before.

Behavior notes:

  • A program that exits without closing the WriteApi now also gets its pending batch written instead of dropped. If InfluxDB is unreachable at that point, exit can take up to max_retry_time while the write is retried.
  • On Python 3.7 and 3.8 the thread pools still shut down through atexit, after the application's handlers, so the bug doesn't occur there and this code path isn't reached.
  • fix: ApiClient finalizer during interpreter shutdown #720 also hardened interpreter shutdown, in ApiClient.__del__. This is a different code path.

Checklist

  • CHANGELOG.md updated
  • Rebased/mergeable
  • A test has been added if appropriate
  • pytest tests/test_WriteApiBatching.py completes successfully (30 passed, including the new tests)
  • Commit messages are conventional
  • Sign CLA (if not already signed)

Tests

BatchingWriteInterpreterShutdownTest in tests/test_WriteApiBatching.py runs 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 the WriteApi is closed in an atexit handler.
  • test_write_and_close_in_atexit: a point written inside the atexit handler 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).

pytest tests/test_WriteApiBatching.py -k InterpreterShutdown

AI/LLM disclosure

  • AI coding tools (Claude Code) were used to trace the shutdown order, draft the change and the tests, and write this description.
  • I reviewed the complete change, understand the reasoning, and ran the reported local tests before submitting.
  • AI output was not pasted unreviewed.

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-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 50.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 90.47%. Comparing base (ef60f89) to head (c1f12bf).

Files with missing lines Patch % Lines
influxdb_client/client/write_api.py 50.00% 1 Missing ⚠️
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

atexit doesn’t work properly in batch mode: "cannot schedule new futures after interpreter shutdown"

2 participants