Skip to content

Session Management Refactoring, Performance Optimization, and Race Condition Fixes#280

Merged
dmytro-landiak merged 42 commits into
mainfrom
session-management
Jan 8, 2026
Merged

Session Management Refactoring, Performance Optimization, and Race Condition Fixes#280
dmytro-landiak merged 42 commits into
mainfrom
session-management

Conversation

@dmytro-landiak
Copy link
Copy Markdown
Contributor

@dmytro-landiak dmytro-landiak commented Dec 30, 2025

Pull Request description

This PR introduces major improvements to the Client Session management flow. The primary focus is on performance optimization (reducing memory allocation/GC pressure), fixing race conditions during high-concurrency scenarios (specifically client takeover), and ensuring cluster consistency.

It also introduces a robust mechanism for handling "ghost" sessions and refactors the RateLimitService for better encapsulation.

🚀 Key Changes

⚡ Performance & Memory Optimization

  • Reduced Garbage Generation: Major refactoring of session management to maximize object reuse and significantly lower memory footprint.
  • Optimized Disconnect Handling: Prevented processing of double/redundant disconnect messages during client session takeovers, reducing unnecessary request generation.
  • Persistent Client Optimization: Improved the connect flow for persistent device clients to avoid unnecessary clearance of persistent messages.

🔒 Concurrency & Race Conditions

  • Session Limits: Fixed race conditions in session limit and application client limit quota checks by moving logic to the correct execution phase.
  • Sequential Limit Accounting: Ensured session limits are incremented/decremented in a strict sequential order per clientId to prevent accounting errors during concurrent access.
  • Auth Persistence: Fixed race conditions in Basic, X.509, JWT and SCRAM auth provider persistence (save/remove operations) when the same client performs a takeover.
  • Client Session Details Consistency: Fixed race conditions regarding the persistence and clearance of MQTT client versions and credentials.

🌐 Cluster Consistency & Reliability

  • Ghost Session Management: Introduced a forceful client session cleanup mechanism (and state correction) to resolve "ghost" sessions effectively.
  • Cluster View Sync: Fixed an issue where different TBMQ nodes in the cluster could hold inconsistent views of active sessions.
  • Disconnect Reasons: Improved logic to correctly propagate disconnect reasons and connection failure codes to the client.

🛠 Refactoring & Design

  • Client Flow Improvements: General improvements to the internal connect and disconnect logic flows.
  • RateLimitService: Refactored to strictly expose business logic methods while encapsulating internal processing logic.

General checklist

  • You have reviewed the guidelines document.
  • Labels that classify your pull request have been added.
  • The milestone is specified and corresponds to fix version.
  • Description references specific issue.
  • Description contains human-readable scope of changes.
  • Description contains brief notes about what needs to be added to the documentation.
  • No merge conflicts, commented blocks of code, code formatting issues.
  • Changes are backward compatible or upgrade script is provided.

Front-End feature checklist

  • Screenshots with affected component(s) are added. The best option is to provide 2 screens: before and after changes;
  • If you change the widget or other API, ensure it is backward-compatible or upgrade script is present.

Back-End feature checklist

  • Added corresponding unit and/or integration test(s). Provide written explanation in the PR description if you have failed to add tests.
  • If new dependency was added: the dependency tree is checked for conflicts.

…t stale sessions and different view in the cluster
…ssion disconnect on cluster session takeover
…n cluster manager to fix wrong put and evict
@dmytro-landiak dmytro-landiak changed the title Session management Session Management Refactoring, Performance Optimization, and Race Condition Fixes Jan 7, 2026
@dmytro-landiak dmytro-landiak added Enhancement New feature or request Core Minor improvement to Core services labels Jan 7, 2026
@dmytro-landiak dmytro-landiak added this to the 2.3.0 milestone Jan 7, 2026
@dmytro-landiak dmytro-landiak added the Bug Something isn't working label Jan 7, 2026
@dmytro-landiak dmytro-landiak merged commit 4131b76 into main Jan 8, 2026
3 checks passed
@dmytro-landiak dmytro-landiak deleted the session-management branch January 8, 2026 09:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug Something isn't working Core Minor improvement to Core services Enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants