fix(pool): claim distinct session slots on Windows - #198
Merged
Conversation
_acquire_session guarded its advisory locking behind `if fcntl is None` and returned pool[0] on Windows. Every concurrent client there was handed the same session string, which is precisely the collision the pool exists to prevent: Telegram sees one auth key on two connections and revokes it permanently for both clients. singleton.py already solves this, locking through msvcrt on Windows and fcntl on POSIX, so expose those primitives as try_lock_exclusive/ release_lock and have the pool use them rather than keeping a second, POSIX-only copy of the same idea. Two details the Windows path needs: the lock file is opened "a+" instead of "w", because the lock covers the first byte and truncating a file another live client holds is refused; and the pid marker is rewritten in place rather than appended to on every start. tests/test_session_pool.py simulated a rival client with a direct fcntl import, so it could not run on Windows at all. It now uses the same primitive as the code under test. The case asserting that a missing fcntl yields pool[0] encoded the bug, so it is replaced by one asserting the claimed slot is genuinely locked against another client. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
chigwell
approved these changes
Aug 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #172, which fixed the same POSIX-only pattern in the session lock. The session pool still has it.
The bug
_acquire_sessionguards its advisory locking behindif fcntl is Noneand returnspool[0]:fcntlis POSIX-only, so on Windows every concurrent client is handed the same session string. That is exactly the collision the pool exists to prevent — Telegram sees one auth key on two connections and revokes it permanently, for both clients. Configuring a pool on Windows today therefore looks like a fix while reproducing the original fault.This is not theoretical: some MCP hosts run a separate copy of the server per session, so two live clients against one account is the normal case, not an edge case.
The fix
singleton.pyalready solves cross-platform advisory locking (msvcrton Windows,fcntlon POSIX). This exposes those primitives astry_lock_exclusive/release_lockand has the pool use them, rather than keeping a second, POSIX-only copy of the same idea.Two details the Windows path needs:
"a+"rather than"w"— the lock covers the first byte, and truncating a file another live client holds is refusedTests
tests/test_session_pool.pysimulated a rival client via a directimport fcntl, so it could not run on Windows at all (ModuleNotFoundError). It now uses the same primitive as the code under test.test_acquire_session_without_fcntl_uses_firstasserted the buggy behaviour, so it is replaced bytest_acquire_session_locks_are_visible_to_other_clients, which asserts the claimed slot is genuinely locked against another client.Verified on Windows 11 / Python 3.13:
tests/test_session_pool.py: 9 passed (previously 2 errored on the fcntl import)🤖 Generated with Claude Code