Skip to content

fix(app-server): keep cancellation responsive under event backpressure - #654

Closed
GraciousGazelles wants to merge 8 commits into
mainfrom
successor/pr605-delivery-core
Closed

GraciousGazelles wants to merge 8 commits into
mainfrom
successor/pr605-delivery-core

Conversation

@GraciousGazelles

Copy link
Copy Markdown
Collaborator

Summary

Validation

  • Candidate head: 81cd3424f5c578008563a8a8d2fa12090f99ef84
  • Base: 9c7f7090a8e8cd958bc7bd917e7b0c01c932038a
  • Hosted validation and exact-SHA review are required before landing.

@gemini-code-assist

Copy link
Copy Markdown

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

Comment thread codex-rs/app-server/src/in_process.rs Resolved
// A single bounded FIFO keeps notification and server-request order
// intact without allowing a stalled event consumer to block command
// routing or accumulate unbounded peer-owned payloads.
let (forward_tx, mut forward_rx) = mpsc::channel::<InProcessServerEvent>(channel_capacity);
@GraciousGazelles

Copy link
Copy Markdown
Collaborator Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the in-process app-server client and runtime to improve backpressure handling by separating event/request delivery from ordinary responses using independent bounded lanes. Required notifications and server requests now block under saturation to preserve FIFO custody, while best-effort events are dropped and reported via lagged markers. Feedback points out a critical issue in the runtime event forwarder where attempting to send a lagged marker on a full queue blocks the task, defeating the backpressure avoidance. The reviewer suggests accumulating skipped events locally and sending the marker only when space is available or before a lossless event.

Comment on lines +533 to +580
let mut event_forward_handle = tokio::spawn(async move {
loop {
let event = tokio::select! {
_ = event_tx_forward.closed() => {
let _ = event_terminal_tx_forward
.try_send(RuntimeEventTerminal::ConsumerClosed);
break;
}
event = forward_rx.recv() => {
let Some(event) = event else { break; };
event
}
};
if event_requires_delivery(&event)
|| matches!(&event, InProcessServerEvent::ServerRequest(_))
{
if event_tx_forward.send(event).await.is_err() {
let _ = event_terminal_tx_forward
.try_send(RuntimeEventTerminal::ConsumerClosed);
break;
}
continue;
}

let (writer_tx, mut writer_rx) = mpsc::channel::<QueuedOutgoingMessage>(channel_capacity);
if let Err(send_error) = event_tx_forward.try_send(event) {
match send_error {
mpsc::error::TrySendError::Full(_) => {
warn!("dropping in-process server event (queue full)");
if event_tx_forward
.send(InProcessServerEvent::Lagged { skipped: 1 })
.await
.is_err()
{
let _ = event_terminal_tx_forward
.try_send(RuntimeEventTerminal::ConsumerClosed);
break;
}
}
mpsc::error::TrySendError::Closed(_) => {
let _ = event_terminal_tx_forward
.try_send(RuntimeEventTerminal::ConsumerClosed);
break;
}
}
}
}
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Issue: Event Forwarder Blocks on Best-Effort Event Drops

In the event forwarder task, when a best-effort event fails to be sent because the queue is full (TrySendError::Full), the code drops the event but immediately attempts to send an InProcessServerEvent::Lagged { skipped: 1 } using .send().await:

if event_tx_forward
    .send(InProcessServerEvent::Lagged { skipped: 1 })
    .await
    .is_err()

Because the queue is full, this .send().await will block the event forwarder task. Blocking the event forwarder task on a best-effort event drop defeats the purpose of dropping best-effort events to avoid backpressure. It causes the forwarder to stop draining forward_rx, which will quickly fill up forward_tx and eventually block the main processor loop on forward_tx.reserve().

Additionally, if multiple best-effort events are dropped consecutively, it will send multiple Lagged { skipped: 1 } events individually (each blocking the task) instead of accumulating them into a single Lagged { skipped: N } event.

Suggested Solution

Accumulate skipped_events in a local variable inside the forwarder loop, and only attempt to send the Lagged marker when a lossless event is about to be sent or when the queue has space again, matching the robust logic used in lib.rs.

        let mut event_forward_handle = tokio::spawn(async move {
            let mut skipped_events = 0usize;
            loop {
                let event = tokio::select! {
                    _ = event_tx_forward.closed() => {
                        let _ = event_terminal_tx_forward
                            .try_send(RuntimeEventTerminal::ConsumerClosed);
                        break;
                    }
                    event = forward_rx.recv() => {
                        let Some(event) = event else { break; };
                        event
                    }
                };
                if skipped_events > 0 {
                    if event_requires_delivery(&event)
                        || matches!(&event, InProcessServerEvent::ServerRequest(_))
                    {
                        if event_tx_forward
                            .send(InProcessServerEvent::Lagged { skipped: skipped_events })
                            .await
                            .is_err()
                        {
                            let _ = event_terminal_tx_forward
                                .try_send(RuntimeEventTerminal::ConsumerClosed);
                            break;
                        }
                        skipped_events = 0;
                    } else {
                        match event_tx_forward.try_send(InProcessServerEvent::Lagged { skipped: skipped_events }) {
                            Ok(()) => {
                                skipped_events = 0;
                            }
                            Err(mpsc::error::TrySendError::Full(_)) => {
                                skipped_events = skipped_events.saturating_add(1);
                                warn!("dropping in-process server event (queue full)");
                                continue;
                            }
                            Err(mpsc::error::TrySendError::Closed(_)) => {
                                let _ = event_terminal_tx_forward
                                    .try_send(RuntimeEventTerminal::ConsumerClosed);
                                break;
                            }
                        }
                    }
                }

                if event_requires_delivery(&event)
                    || matches!(&event, InProcessServerEvent::ServerRequest(_))
                {
                    if event_tx_forward.send(event).await.is_err() {
                        let _ = event_terminal_tx_forward
                            .try_send(RuntimeEventTerminal::ConsumerClosed);
                        break;
                    }
                    continue;
                }

                if let Err(send_error) = event_tx_forward.try_send(event) {
                    match send_error {
                        mpsc::error::TrySendError::Full(_) => {
                            skipped_events = skipped_events.saturating_add(1);
                            warn!("dropping in-process server event (queue full)");
                        }
                        mpsc::error::TrySendError::Closed(_) => {
                            let _ = event_terminal_tx_forward
                                .try_send(RuntimeEventTerminal::ConsumerClosed);
                            break;
                        }
                    }
                }
            }
        });

Copy link
Copy Markdown
Collaborator Author

Closing this historical candidate as superseded by the landed current-main successors #674 and #747. Their merged implementations cover the accepted response-lane liveness, bounded event/FIFO delivery, lag accounting, consumer-close custody, auth rejection, and shutdown-progress behavior on current main. The branch and commit history are intentionally preserved for audit; no source rewrite or branch deletion is performed.

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