Skip to content

fix(foundation): add TTL expiry and cancel_request to DeferredQueue to prevent memory leaks#1578

Open
SH20RAJ wants to merge 1 commit intomofa-org:mainfrom
SH20RAJ:fix/1569-deferred-queue-leak
Open

fix(foundation): add TTL expiry and cancel_request to DeferredQueue to prevent memory leaks#1578
SH20RAJ wants to merge 1 commit intomofa-org:mainfrom
SH20RAJ:fix/1569-deferred-queue-leak

Conversation

@SH20RAJ
Copy link
Copy Markdown
Contributor

@SH20RAJ SH20RAJ commented Apr 4, 2026

Summary

Fixes #1569 - DeferredQueue was leaking requests on client disconnection, filling up the queue with "ghost" requests and preventing new legitimate requests from being enqueued.

Problem

The DeferredQueue had no mechanism to:

  1. Explicitly cancel requests when callers disconnect
  2. Remove old requests that are never processed

Scenario

  1. Submit 100+ requests exceeding defer_threshold
  2. All enqueued in DeferredQueue (fills max_size)
  3. Callers timeout or disconnect
  4. Memory becomes available
  5. New request submitted

Expected: New request accepted or enqueued
Actual: Rejected because queue full of "ghost" requests

Solution

1. TTL-Based Expiry (default 5 minutes)

Added to DeferredRequest:

pub ttl: Duration,
pub fn is_expired(&self) -> bool { ... }
pub fn with_ttl(id, required_mb, ttl) -> Self { ... }

Requests automatically expire after TTL, preventing indefinite accumulation.

2. Explicit Cancellation

Added to DeferredQueue:

pub fn cancel_request(&mut self, request_id: &str) -> bool { ... }

Orchestration layer or scheduler can cancel specific requests when callers disconnect.

3. Updated Dequeue Logic

  • Skip expired requests in dequeue_oldest_fitting()
  • New drain_ttl_expired() method for background cleanup

4. Comprehensive Tests

  • test_cancel_request() - cancel and verify removal
  • test_ttl_expiry() - requests expire after TTL
  • test_drain_ttl_expired() - background TTL cleanup
  • test_ghost_request_prevention() - full scenario fix

Impact

✅ Prevents DoS via filled queue from disconnected callers
✅ Automatic cleanup without manual intervention
✅ Graceful support for both explicit + automatic expiry
✅ Backward compatible (default TTL = 5 min)

Testing

All new tests pass. Backwards compatible with existing code.

GSOC 2026 Contribution

Addresses critical P1 issue #1569. Demonstrates:

  • DoS prevention & security thinking
  • Defensive programming patterns
  • Comprehensive testing discipline

Cc: @Mustafa11300 @rahulkr182

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.

[BUG] DeferredQueue leaks requests on client disconnection, leading to permanent DoS

1 participant