Repository navigation
fix(studio/colab): fix OutStream startup crash and tidy the notebook cards - #7404
danielhanchen merged 10 commits into
Conversation
Unsloth Studio crashed at server startup on Colab with: Unsloth Studio failed to start: 'OutStream' object has no attribute 'watch_fd_thread' Root cause: - Colab's ipykernel OutStream is created with watchfd=False, so it never gains a watch_fd_thread. The OutStream.close() in the affected ipykernel versions joins that thread unconditionally and raises AttributeError (ipython/ipykernel#867). - _setup_server_disk_logging() replaces sys.stdout/sys.stderr with a tee. That changes the console object identity, so Colab's absl logging handler (which captured the original OutStream and whose close() deliberately skips sys.stdout/sys.stderr) no longer treats it as the live console. - run_server builds uvicorn.Config(...), whose configure_logging runs logging.config.dictConfig -> logging.shutdown, closing every existing handler. The absl handler then calls close() on the orphaned OutStream and the AttributeError propagates out of uvicorn.Config and aborts startup. Fix: - Before installing the tee, harden the displaced console streams' close() so only the ipykernel#867 AttributeError is swallowed; a healthy close() runs unchanged and any other error still propagates. The buggy close() raises before it nulls pub_thread, so the stream stays fully usable. - Give _TeeStream its own close() that flushes the log copy and forwards close() to the wrapped console stream best-effort, so a handler that captured the tee cannot crash startup either. Add regression tests reproducing the exact path (an absl-style handler closing a watchfd=False OutStream stand-in during logging.shutdown) and asserting the tee/console path survives and keeps logging.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 89c2513b56
ℹ️ 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| def _safe_close(*args, **kwargs): | ||
| try: | ||
| return _orig_close(*args, **kwargs) | ||
| except AttributeError: | ||
| # ipython/ipykernel#867: watchfd=False OutStream.close() joins a | ||
| # thread that was never created. Nothing to clean up; keep going. | ||
| return None |
There was a problem hiding this comment.
Restrict suppression to the known missing watch_fd_thread error
_safe_close catches every AttributeError, although the hardening is intended only for the watch_fd_thread defect. If another console implementation (or a different OutStream.close() failure) raises AttributeError while performing real teardown, Uvicorn's logging shutdown will now treat that failed close as successful, hiding the error and potentially leaving a partially closed console/resource behind. Check that this is specifically the missing watch_fd_thread case (for example, the expected message/attribute) and re-raise other AttributeErrors.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Good catch, fixed in ed81aec. _safe_close now re-raises anything that is not ipython/ipykernel#867: it matches on the attribute name (AttributeError.name from Python 3.10, else the message, which also covers the 3.12 "Did you mean" tail), which is the only attribute the pre-1076 OutStream.close() touches unguarded. Regression tests cover the swallowed #867 case, the message-shape variant, and an unrelated AttributeError from close() still propagating.
|
@codex review |
|
Codex Review: Didn't find any major issues. What shall we delve into next? Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
@codex review |
|
Codex Review: Didn't find any major issues. 🎉 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
@codex review |
|
Codex Review: Didn't find any major issues. Delightful! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Starting Unsloth Studio in Colab failed before the server ever came up:
This fixes that crash, and cleans up the notebook cards that render once Studio is running.
Startup crash
Three things line up to produce it:
ipykernel.iostream.OutStreamwithwatchfd=False, sowatch_fd_threadis never set, butOutStream.close()in the shipped version callsself.watch_fd_thread.join()unconditionally on Linux (ipykernel On train_on_responses_only #867)._setup_server_disk_logging()swapssys.stdoutandsys.stderrfor a_TeeStream, which changes the console object identity.OutStream, and itsclose()only skips streams still equal to the currentsys.stdout/sys.stderr. Once the tee replaces those, the original is no longer treated as user managed. Buildinguvicorn.Configthen runsdictConfig->logging.shutdown, absl closes the orphanedOutStream, and theAttributeErrorpropagates out ofrun_server.Fix:
_harden_console_close()wrapsclose()on the real stdout and stderr before the tee is installed, swallowing only the ipykernel On train_on_responses_only #867AttributeError. Healthy closes run in full, any other error still propagates, and it no-ops whenclosecannot be reassigned. Off Colab it does nothing._TeeStream.close()flushes the log copy and forwards the close best effort, so a handler holding the tee itself also cannot break startup.The affected ipykernel raises before it clears
pub_thread, so the stream stays usable after the swallowed error.Notebook cards
A
start(cloudflare=True)run rendered three cards. The password the user needs sat two cards away from the button that needs it, and the third card had nothing left to say once a tunnel was up._shareable_link_htmltakes optionalpasswordandusername.Username:field. The admin user is always the default, so it reads inline as "Log in asunslothwith this password" in both cards.*.prod.colab.devproxy URL that 404s outside the notebook tab. It still renders on every other path, where it carries the real fallback guidance.Tests
test_server_disk_logging_outstream.py: 14 new tests covering the baseline crash throughlogging.shutdown, survival with the hardening plus tee, and the harden and tee semantics.test_colab_embed.py: password ordered after the link, no duplicate login card when folded in, the login card still rendering without a tunnel, and the ready card skipped only when a tunnel is up.test_colab_embed.py,test_server_disk_logging_outstream.pyandtest_server_disk_logging.py. The wider run.py backed suite is green at 980.