Skip to content

fix(python-sdk): notify on_exit after immediate watch stop - #2072

Open
Elon-Jiang wants to merge 1 commit into
e2b-dev:mainfrom
Elon-Jiang:fix/python-watch-immediate-stop
Open

Elon-Jiang wants to merge 1 commit into
e2b-dev:mainfrom
Elon-Jiang:fix/python-watch-immediate-stop

Conversation

@Elon-Jiang

@Elon-Jiang Elon-Jiang commented Oct 31, 2026 •

Copy link
Copy Markdown

Summary

Fixes #2071.

await handle.stop() can run immediately after await sandbox.files.watch_dir(...), before the handle's background task takes its first step. Cancelling that task prevents _handle_events() from running at all, so its cancellation handler never sends on_exit(None).

Have stop() ensure the exit callback runs after stopping the task and closing the stream. Guard the shared callback helper so repeat stops, normal completion, and a reported stream error cannot send another notification or replace the original error. Async callbacks are still awaited, and callback exceptions retain the existing handling.

Add public Filesystem.watch_dir regressions for immediate stop with sync/async exit callbacks, repeated stops, and stopping after clean/error completion. Include a Python SDK patch changeset.

Usage

handle = await sandbox.files.watch_dir(
    "/home/user",
    on_event=lambda event: print(event),
    on_exit=lambda error: print("watch ended", error),
)
await handle.stop()  # on_exit(None) also runs if the task has not started yet

Validation

Python 3.12.14, main 8cc9ed1e30a57d89cefaec112680567d62f21112:

  • Before the fix: the two immediate-stop regression cases fail; 11 other watch-handle cases pass.
  • After the fix: all 13 watch-handle cases pass.
  • All 32 root-level Python unit-test modules: 489 passed (pytest tests/test_*.py -q from the Python SDK).
  • Independent local Connect/HTTP2 test using Filesystem.watch_dir, the generated RPC client and real SDK codec/interceptors: on_exit runs exactly once after immediate/repeated stop, and the server receives RST_STREAM CANCEL.
  • pnpm --filter @e2b/python-sdk run format: 444 files unchanged.
  • pnpm --filter @e2b/python-sdk run lint: passed.
  • pnpm --filter @e2b/python-sdk run typecheck: passed.
  • uv build with the repository-pinned uv 0.13.4: sdist and wheel passed.
  • Cloud sandbox integration tests were not run.

The scheduling issue is specific to Python's newly created async task. JS starts its handler in the constructor; sync Python uses polling and has no exit callback. Their implementations are unchanged.

Filesystem.watch_dir already documents on_exit(None) on stop; this restores that existing contract and adds a changelog entry. I could not access e2b-dev/docs (GitHub API 404), so no separate docs PR is attached.

AI assistance was used to investigate, implement, and validate this change.


Devin Review

@cla-bot

cla-bot Bot commented Oct 31, 2026

Copy link
Copy Markdown

We require contributors to sign our Contributor License Agreement, and we don't have @Elon-Jiang on file. You can sign our CLA at https://e2b.dev/docs/cla . Once you've signed, post a comment here that says '@cla-bot check'

@changeset-bot

changeset-bot Bot commented Oct 31, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: cd02a8c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@e2b/python-sdk Patch
@e2b/code-interpreter-python Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 31, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-31T01:56:36.729201Z
We couldn’t complete this review. Comment @codex review to try again.
cd02a8c PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TASTE.md check (T-1/T-2 parity, T-26/T-27/T-28 handles and callbacks, T-30 stop(), T-69 docs): no violations found. Keeping this change to async Python is fine: JS starts its event pump in the constructor, and sync Python has no on_exit (T-28).

Written by Devin

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Devin Review

@Elon-Jiang

Copy link
Copy Markdown
Author

@cla-bot check

@cla-bot

cla-bot Bot commented Oct 31, 2026

Copy link
Copy Markdown

We require contributors to sign our Contributor License Agreement, and we don't have @Elon-Jiang on file. You can sign our CLA at https://e2b.dev/docs/cla . Once you've signed, post a comment here that says '@cla-bot check'

@cla-bot

cla-bot Bot commented Oct 31, 2026

Copy link
Copy Markdown

The cla-bot has been summoned, and re-checked this pull request!

@jgfurlan

jgfurlan commented Nov 5, 2026

Copy link
Copy Markdown

@Elon-Jiang, the CLA check still appears unsuccessful. Is the verification issue already being handled?

@Elon-Jiang

Copy link
Copy Markdown
Author

@cla-bot check

@cla-bot cla-bot Bot added the cla-signed label Nov 6, 2026
@cla-bot

cla-bot Bot commented Nov 6, 2026

Copy link
Copy Markdown

The cla-bot has been summoned, and re-checked this pull request!

This branch has not been deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Async Python watch_dir immediate stop skips on_exit

2 participants