Skip to content

feat(on-early-drop): Add middleware for client early drop detection#636

Open
fbergero wants to merge 1 commit intotower-rs:mainfrom
fbergero:on-early-drop-396
Open

feat(on-early-drop): Add middleware for client early drop detection#636
fbergero wants to merge 1 commit intotower-rs:mainfrom
fbergero:on-early-drop-396

Conversation

@fbergero
Copy link

@fbergero fbergero commented Jan 9, 2026

Add on-early-drop middleware for client early drop detection

Introduces a new middleware that detects when a client disconnects before receiving a full response. This allows for monitoring and logging of early connection terminations.

Fixes #396

Motivation

When clients disconnect before receiving a complete response (e.g., due to browser navigation, timeouts, or network issues), the entire future chain is typically dropped with no indication of what happened. This creates "disappearing requests" that never show up in logs or metrics, making it difficult to:

  1. Identify patterns of client disconnects
  2. Properly monitor the full request lifecycle
  3. Handle or recover from incomplete operations

Solution

This PR introduces two main components:

1. OnEarlyDropGuard

A standalone guard that executes a callback function when dropped, unless explicitly marked as completed:

  • Simple API: Create the guard with a closure that executes on early drop
  • Callback Control: Mark operations as successfully completed to prevent the callback
  • FnOnce Support: Designed to work with closures that take ownership of their environment

2. OnEarlyDropLayer/Service

A Tower middleware that applies early drop detection to HTTP services:

  • Request Context: Closures can capture specific request information

This implementation enables various use cases:

  • Logging dropped requests with context
  • Capturing metrics about client disconnections
  • Maintaining trace context across early terminations
  • Performing cleanup operations for abandoned requests

@fbergero fbergero marked this pull request as ready for review January 9, 2026 20:01
Introduces a new middleware that detects when a client disconnects
before receiving a full response. This allows for monitoring and logging
of early connection terminations.

Fixes tower-rs#396
@rcoh
Copy link

rcoh commented Jan 28, 2026

cc @tottoto can you review?

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.

tower_http::trace::OnEarlyDrop

2 participants