-
Notifications
You must be signed in to change notification settings - Fork 15
fix: session/ci/appid de-conflation #909
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
let session_id_clone = session_id.clone(); | ||
tokio::spawn(async move { | ||
if let Err(e) = callback.sender.send(output_clone).await { | ||
error!( |
Check failure
Code scanning / CodeQL
Cleartext logging of sensitive information High
session_id_clone
} | ||
}); | ||
|
||
debug!("Event {} sent to client {}", method, session_id); |
Check failure
Code scanning / CodeQL
Cleartext logging of sensitive information High
session_id
// Check if we already have a Thunder subscription for this method | ||
if let Some(sub_state) = method_subscriptions.get_mut(&event_method_key) { | ||
// Thunder subscription exists, just add this client to the fanout list | ||
debug!( |
Check failure
Code scanning / CodeQL
Cleartext logging of sensitive information High
session_id_str
if !clients.contains(&session_id_str) { | ||
clients.push(session_id_str.clone()); | ||
sub_state.client_count += 1; | ||
debug!( |
Check failure
Code scanning / CodeQL
Cleartext logging of sensitive information High
session_id_str
} | ||
} else { | ||
// No Thunder subscription exists, need to create one | ||
debug!( |
Check failure
Code scanning / CodeQL
Cleartext logging of sensitive information High
session_id_str
} | ||
} else { | ||
// Client wants to unsubscribe | ||
debug!( |
Check failure
Code scanning / CodeQL
Cleartext logging of sensitive information High
session_id_str
Critical Memory Leak Fix: - Fixed incomplete session cleanup in remove_session_from_events() - Changed from removing only first EventListener to removing ALL listeners per session - Used retain() instead of manual remove(index) for comprehensive cleanup - Added test_remove_session_removes_all_listeners() to prevent regression Memory leak was causing 1-4KB retention per session with multiple event listeners, scaling linearly with Firebolt API usage. This was particularly problematic on embedded aarch64 devices with constrained memory. Struct Alignment Optimizations: - Optimized CallContext field ordering to reduce padding (~8 bytes saved per instance) - Eliminated SessionData wrapper in Session struct for better cache locality - Reordered PendingSessionInfo fields to minimize internal padding - Improved memory alignment for frequently allocated structures Impact: - Eliminates proportional memory growth with Firebolt API calls - Reduces per-EventListener memory overhead by 8+ bytes - Improves cache locality and reduces heap fragmentation - Critical for embedded deployment on memory-constrained devices All tests pass (27 session-related tests), no functional regressions. Validated with clippy and comprehensive test suite.
Minimum allowed line rate is |
What
This PR fixes a critical HashMap memory leak in the Firebolt gateway session management and introduces a comprehensive type-safe identifier system to prevent similar issues in the future.
Key Changes:
firebolt_gateway.rs
where session cleanup used inconsistent identifier keystypes.rs
forSessionId
,ConnectionId
,AppId
,AppInstanceId
,RequestId
, andDeviceSessionId
Why
Memory Leak Issue:
The original code had a critical HashMap memory leak where session registration used
session_id
as the key, but session cleanup usedconnection_id
, resulting in sessions never being properly removed from memory. This could lead to unbounded memory growth in production environments with high session turnover.Type Safety Need:
The codebase frequently conflated different identifier types (session IDs, connection IDs, app IDs) using plain
String
types, making it easy to accidentally use the wrong identifier and creating potential for similar bugs in the future.How
Immediate Fix:
UnregisterSession
handler infirebolt_gateway.rs
to consistently usecid
(connection ID) for all cleanup operations, matching the key used during registrationregister(cid, session)
→cleanup(cid)
Long-term Prevention:
#[serde(transparent)]
for JSON compatibilityDebug
,Clone
,PartialEq
,Eq
,Hash
,FromStr
,TryFrom
)Type System Design: