Skip to content

fix(studio/colab): fix OutStream startup crash and tidy the notebook cards - #7404

Merged
danielhanchen merged 10 commits into
unslothai:mainfrom
danielhanchen:fix/studio-colab-outstream-watch-fd
Jul 25, 2026
Merged

danielhanchen merged 10 commits into
unslothai:mainfrom
danielhanchen:fix/studio-colab-outstream-watch-fd

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Jul 24, 2026 •

Copy link
Copy Markdown
Member

Starting Unsloth Studio in Colab failed before the server ever came up:

run_server pre-uvicorn setup completed in 328.9ms
Unsloth Studio failed to start: 'OutStream' object has no attribute 'watch_fd_thread'

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:

  1. Colab builds its ipykernel.iostream.OutStream with watchfd=False, so watch_fd_thread is never set, but OutStream.close() in the shipped version calls self.watch_fd_thread.join() unconditionally on Linux (ipykernel On train_on_responses_only #867).
  2. _setup_server_disk_logging() swaps sys.stdout and sys.stderr for a _TeeStream, which changes the console object identity.
  3. absl's logging handler captured the original OutStream, and its close() only skips streams still equal to the current sys.stdout / sys.stderr. Once the tee replaces those, the original is no longer treated as user managed. Building uvicorn.Config then runs dictConfig -> logging.shutdown, absl closes the orphaned OutStream, and the AttributeError propagates out of run_server.

Fix:

  • _harden_console_close() wraps close() on the real stdout and stderr before the tee is installed, swallowing only the ipykernel On train_on_responses_only #867 AttributeError. Healthy closes run in full, any other error still propagates, and it no-ops when close cannot 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.

  • The password now renders inside the shareable link card, directly under the link and the Open Unsloth Studio button. _shareable_link_html takes optional password and username.
  • Dropped the Username: field. The admin user is always the default, so it reads inline as "Log in as unsloth with this password" in both cards.
  • The standalone login card only renders when there is no link card to fold it into, which is the tunnel requested but failed path. The normal path now shows one card instead of two.
  • Skip the ready card on Colab when a tunnel is up. The in-cell proxy embed is already skipped in that case, so the card only repeated the link card above it and printed a truncated *.prod.colab.dev proxy 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 through logging.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.
  • 50 pass across test_colab_embed.py, test_server_disk_logging_outstream.py and test_server_disk_logging.py. The wider run.py backed suite is green at 980.

danielhanchen and others added 2 commits July 24, 2026 10:05
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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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".

Comment thread studio/backend/run.py
Comment on lines +1043 to +1049
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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

@danielhanchen danielhanchen changed the title fix(studio/colab): survive ipykernel OutStream close() during Studio startup fix(studio/colab): fix OutStream startup crash and tidy the notebook cards Jul 25, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: ed81aec924

ℹ️ 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".

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: 43ef32d913

ℹ️ 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".

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: 712df8a1a9

ℹ️ 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".

@danielhanchen
danielhanchen merged commit c4b7772 into unslothai:main Jul 25, 2026
29 of 34 checks passed
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.

1 participant