Skip to content

Studio: stop asking for OAuth consent again when reconnecting to an MCP server - #18171

Merged
danielhanchen merged 5 commits into
unslothai:mainfrom
NilayYadav:fix-mcp-oauth-refresh-race
Nov 18, 2026
Merged

danielhanchen merged 5 commits into
unslothai:mainfrom
NilayYadav:fix-mcp-oauth-refresh-race

Conversation

@NilayYadav

@NilayYadav NilayYadav commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

Related to #18164 (the reported bug is fixed in #18203). When several connections to Notion's MCP server refreshed an expired token at the same time, Studio could keep a refresh token Notion had already replaced. The next refresh failed with a 400 and Studio asked for consent again on every connection. Now one connection refreshes and the rest reuse the new token.

Tested against the real Notion MCP server in Studio:

Before After
before after

@bezabyte

Copy link
Copy Markdown
Contributor

I audited this against our #18176: the PRs cover different failure modes in #18164. This PR serializes concurrent refresh-token rotation; #18176 preserves the registered DCR client and redirect URI when re-authorization is required. They are complementary, not exact duplicates.

danielhanchen added a commit that referenced this pull request Nov 17, 2026
… with its output (rest-2 timeouts) (#18180)

`tests/test_ddp_find_unused_default.py::test_trainable_embeddings_train_under_ddp_reentrant_checkpointing`
hits pytest-timeout in the CPU `rest-2` shard on unrelated PRs (#18135,
#18165, #18171 today):

```
tests/test_ddp_find_unused_default.py:285: in test_trainable_embeddings_train_under_ddp_reentrant_checkpointing
    proc = subprocess.run(
E   Failed: Timeout (>330.0s) from pytest-timeout.
```

The test takes about 11 s locally and hung only on some workers, with no
child output in the log.

## Cause

The test copies `os.environ` into both children: the single-process
reference and the `torchrun` pair. The worker decides it is distributed
from `WORLD_SIZE`. Several tests write rank variables (`RANK`,
`WORLD_SIZE`, `MASTER_PORT`, ...) into the pytest process environment,
so on a worker that ran one of them first, the single-process reference
sees `WORLD_SIZE=2`, calls `init_process_group("gloo")` and waits for a
second rank that never comes. It never returns, and the child's `timeout
= 600` is longer than pytest-timeout's 330 s, so CI only showed a bare
timeout.

Reproduced on main by running the file with `WORLD_SIZE=2 RANK=0
MASTER_ADDR=127.0.0.1 MASTER_PORT=29555` in the environment: it hangs
until killed.

## Fix

- `_rank_env(tmp_path)` builds the child environment without `RANK`,
`WORLD_SIZE`, `LOCAL_RANK`, `LOCAL_WORLD_SIZE`, `GROUP_RANK`,
`ROLE_RANK`, `MASTER_ADDR`, `MASTER_PORT` (torchrun sets its own) and
the `UNSLOTH_DDP_FIND_UNUSED_PARAMETERS_AUTO` override the test already
dropped.
- `_run_child` runs each child under a timeout below pytest-timeout (150
s each for the two-step test, 300 s for the single torchrun in the VLM
test) and, if one still hangs, fails with the tail of its output instead
of a bare timeout.
- Both DDP tests in the file use them.

## Verification

- `tests/test_ddp_find_unused_default.py`: 63 passed.
- Same file with `WORLD_SIZE=2 RANK=0 MASTER_ADDR=127.0.0.1
MASTER_PORT=29555` exported: 63 passed (main: hangs).
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-18T05:51:52.944897Z 7471324 Manual request
ℹ️ 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.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: c3141b4f6f

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

@NilayYadav

Copy link
Copy Markdown
Collaborator Author

@codex review

@NilayYadav

Copy link
Copy Markdown
Collaborator Author

@codex security review

@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: a45e12f821

ℹ️ 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 on lines +54 to +55
_oauth_refresh_locks: dict[tuple[int, str], threading.Lock] = {}
_oauth_refresh_locks_guard = threading.Lock()

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 Reset the new OAuth locks after a fork

On POSIX, a child forked while another thread holds either of these locks inherits it in the locked state, but the existing _reset_after_fork() hook resets only the session and cleanup locks. The owning thread no longer exists in the child, so its next OAuth connection either blocks synchronously in _oauth() on the guard or repeatedly times out waiting for the per-URL refresh lock; releasing the parent's copy does not unblock the child. Recreate the guard and clear the refresh-lock table in the existing at-fork hook.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Not reachable: every Studio worker uses spawn, and the only os.fork() callers (sandbox_landlock, sandbox_egress_relay) exec or _exit without touching MCP OAuth.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@NilayYadav

Copy link
Copy Markdown
Collaborator Author

@codex review

@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: f7e8574b0d

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

import time
import uuid
from contextlib import contextmanager
from contextlib import aclosing, contextmanager

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Avoid the Python-3.10-only import

For Studio installs using the explicitly supported Python 3.9 backend (studio/backend/requirements/studio.txt keeps <3.10 dependency pins), this import raises ImportError because contextlib.aclosing is available only in Python 3.10 and later. routes/mcp_servers.py imports mcp_client at module load and main.py includes that router, so the backend fails to start even when OAuth is unused; use a Python 3.9-compatible async-generator close pattern or a guarded fallback.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Not reachable: the backend floor is 3.10 (studio-backend-ci PYTHON_FLOOR, lint_backend_python_floor), fastmcp>=4.1.5 itself requires Python >=3.10, and main already imports contextlib.aclosing in managed_engine_chat.

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: f7e8574b0d

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

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 747132429e

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

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.

3 participants