Skip to content

Serialise GLPI v1 session renewals so concurrent requests open and kill the session once - #50

Merged
baraline merged 2 commits into
mainfrom
fix/v1-session-renewal-race
Nov 23, 2026
Merged

baraline merged 2 commits into
mainfrom
fix/v1-session-renewal-race

Conversation

@baraline

Copy link
Copy Markdown
Owner

Problem

GLPIV1Session renewed its token without any lock. A client shared by concurrent tasks or threads hit three races:

  • every request that found the token stale at the same time (every 15 minutes by default) ran killSession then initSession;
  • concurrent first calls each opened a session;
  • concurrent 401/403 rejections each renewed.

The second killSession answered 401, the first new session was overwritten and never killed (one leaked server-side session per renewal), and requests in flight could be sent with a token that had just been killed.

Fix

  • A single _session_lock (the library's _concurrency.Lock: asyncio.Lock on the async surface, threading.Lock in the sync twin) serialises acquisition.
  • Each path re-checks under the lock: token still missing, still stale, or still the very token the failed request used. Waiters reuse the token the first renewal obtained.
  • Only _ensure_session, _renew_rejected_session and close take the lock, and none calls another or _headers while holding it, so the non-re-entrant sync lock cannot deadlock. close() still kills the current session and releases the HTTP client.
  • The module docstring states that renewal is serialised.
  • Retry decorators are unchanged. The sync twin is regenerated with unasync_build.py.

Tests

Hand-written on both trees (task bursts on async, thread bursts on sync), listed in HAND_WRITTEN. They cover stale renewal, concurrent first use, concurrent rejections of one token, and close() after a renewal, and assert exactly one killSession and one initSession per renewal, all requests on the new token and no leaked session. All fail without the lock.

pytest -m "not integration" (2316 passed), ruff check, ruff format --check, mypy, unasync_build.py --check and the sphinx -W build pass.

🤖 Generated with Claude Code

baraline and others added 2 commits November 22, 2026 22:57
…ll the session once

A client shared by concurrent tasks or threads renewed the v1 session once per request that found the token stale, on the first lazy initSession, and after a 401 or 403 rejection. Each renewal killed the token and opened a session: the second killSession answered 401, and the first new session was overwritten and never killed, leaking one server-side session per renewal.

Every acquisition now takes a lock (the library's auth Lock type) and re-checks under it whether the work is still needed: token still missing, still stale, or still the very token the failed request used. Waiters reuse the token the first renewal obtained. Only _ensure_session, _renew_rejected_session and close take the lock, and none calls another, so the non-re-entrant sync lock cannot deadlock. The retry decorators are unchanged.

Tests are hand-written on both sides (task bursts on the async tree, thread bursts on the sync tree) and fail without the lock.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.16%. Comparing base (8a02d2c) to head (9b05357).
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #50      +/-   ##
==========================================
+ Coverage   98.11%   98.16%   +0.05%     
==========================================
  Files          96       96              
  Lines        3811     3823      +12     
==========================================
+ Hits         3739     3753      +14     
+ Misses         72       70       -2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

2 participants