Record FedAuth token deterministically in mock server - #91
Merged
Saurabh Singh (saurabh500) merged 3 commits intoJul 6, 2026
Merged
Conversation
The mock server previously recorded connection tokens into the shared ConnectionStore only at connection teardown, and keyed the store by the client socket address. Both flaws made downstream fedauth regression tests flaky on Linux: tokens were not visible when connect() returned (no barrier, papered over with sleeps), and sequential connects reusing the same ephemeral port overwrote each other under the same key. Record the token eagerly during login, before the LoginAck is sent, by giving ConnectionProcessor a handle to the shared store. Since the client blocks on LoginAck, the token is guaranteed visible once connect() returns. Re-key ConnectionStore by a unique per-connection id from an AtomicU64 counter using a BTreeMap so iteration is ordered and the most recent connection is deterministic. Drop the time.sleep workarounds in the Python fedauth tests. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Assert the FedAuth token is visible in the shared ConnectionStore while the client connection is still open, guarding against a regression to teardown-only recording. Mirrors the downstream ODBC pooled-connection scenario where close() does not send EOF. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
📊 Code Coverage Report
Diff CoverageDiff: main...HEAD, staged and unstaged changes
Summary
mssql-mock-tds/src/server.rs🔗 Quick Links |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR makes FedAuth access-token capture in mssql-mock-tds deterministic and queryable immediately after login, addressing Linux-only flakiness caused by teardown-only recording and socket-address key collisions. It also updates upstream tests to stop relying on timing sleeps and adds a Rust regression guard ensuring tokens are visible while the client connection is still open.
Changes:
- Record connection state (incl. FedAuth token) eagerly into a shared store during login, and re-key the store by a monotonic per-connection
conn_id. - Remove
time.sleep(...)workarounds from the Python FedAuth mock-server tests. - Add a Rust regression test asserting the token is recorded before the client connection is closed.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
mssql-mock-tds/src/server.rs |
Adds conn_id-keyed ConnectionStore and eager store upserts during FedAuth login paths. |
mssql-tds/tests/test_mock_server_fedauth.rs |
Adds a regression test to ensure token visibility before connection teardown. |
mssql-py-core/tests/rs-only-tests/test_mock_server_fedauth.py |
Removes timing sleeps now that token recording is eager/deterministic. |
Only used internally by ConnectionStore::store; keep the crate's public surface deliberate per repo conventions. Addresses PR review feedback. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
David Engel (David-Engel)
approved these changes
Jul 6, 2026
Saurabh Singh (saurabh500)
deleted the
saurabh500-mock-tds-deterministic-token-main
branch
July 6, 2026 22:42
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.
Summary
Fixes two design flaws in
mssql-mock-tdsthat made downstream FedAuth regression tests (microsoft/mssql-python#652:test_unique_access_token_transmitted_exactly,test_distinct_tokens_on_sequential_connects) flaky/failing only on Linux.Targets
maindirectly and contains only the mock-server token-capture changes (this supersedes #89, which was stacked on #80).The two flaws
ConnectionProcessorduring login but only copied into the shared, queryableConnectionStoreafter the read loop broke on client EOF. That insert ran on the server task after the client'sclose()had already returned, so tests had no barrier to wait on and papered over it withtime.sleep.ConnectionStorewasHashMap<SocketAddr, ConnectionInfo>. Two sequential connects from the same client process frequently reuse the same ephemeral local port on Linux, so the second connection's insert overwrote the first under the same key —has_received_token(first)then returnedFalse.The fix
LoginAck.ConnectionProcessornow holds anOption<Arc<Mutex<ConnectionStore>>>and upserts its state into the shared store in both FedAuth paths (inline-token Login7 and challengedFedAuthToken) before the response bytes are returned. Because the client blocks on theLoginAckread, the token is guaranteed visible the momentconnect()returns — no teardown dependency, no sleeps. The existing teardownstore.store()calls remain as a harmless final upsert.AtomicU64counter on the server assigns aconn_idataccept()time, threaded into theConnectionProcessor.ConnectionStorenow usesBTreeMap<u64, ConnectionInfo>so iteration and.values().last()are ordered andget_last_access_token()deterministically returns the most recent connection.store()upserts byconn_id.ConnectionInfo.addris retained for info;ConnectionStore::getnow takes aconn_id.time.sleepworkarounds inmssql-py-core/tests/rs-only-tests/test_mock_server_fedauth.py;conn.close()calls kept.test_token_recorded_before_connection_closethat asserts the token is present in the store while the client connection is still open (guards against a regression to teardown-only recording; mirrors the ODBC pooled-connection scenario whereclose()returns the socket to a pool without EOF).Validation
cargo nextest run -p mssql-tds --test test_mock_server_fedauth— 6/6 pass (incl. the new guard).cargo clippy -p mssql-tds --tests --all-features -- -D warnings— clean.rustfmtapplied.