Skip to content

mailbox: fix AwaitRPC lost-wakeup flake - #121

Closed
bhandras wants to merge 1 commit into
mainfrom
fix/mailbox-awaitrpc-lost-wakeup
Closed

mailbox: fix AwaitRPC lost-wakeup flake#121
bhandras wants to merge 1 commit into
mainfrom
fix/mailbox-awaitrpc-lost-wakeup

Conversation

@bhandras

@bhandras bhandras commented Feb 19, 2026

Copy link
Copy Markdown
Member

Summary

This fixes a flaky timeout in TestClient_ConcurrentInFlightDoesNotDrop by
eliminating a lost-wakeup race in AwaitRPC.

Root Cause

AwaitRPC previously did these steps separately:

  1. Check pending for the response.
  2. Register a waiter channel.

If a response arrived between steps 1 and 2, handleEnvelope would cache
the response and notify the current waiter set — which did not yet
include the new waiter. The newly registered waiter would then block until
context timeout.

Fix

Make the response check and waiter registration atomic under one lock via
popPendingOrAddWaiter:

  • if pending response exists: return it immediately and skip waiter registration
  • otherwise: register waiter before releasing the lock

This removes the race window where notifications can be missed.

CI Failure

Observed CI failure (PR #79, unit-cover):

Failure signature:

  • TestClient_ConcurrentInFlightDoesNotDrop
  • context deadline exceeded

Validation

  • Reproduced pre-fix flake locally with high repetition.
  • Post-fix stress runs passed:
    • go test ./mailbox/client -run TestClient_ConcurrentInFlightDoesNotDrop -count=20000 -timeout=60m
    • go test -covermode=atomic -coverpkg=github.com/lightninglabs/darepo-client/... -tags='dev nolog' ./mailbox/client -run TestClient_ConcurrentInFlightDoesNotDrop -count=1000 -timeout=20m

@gemini-code-assist

Copy link
Copy Markdown

Summary of Changes

Hello @bhandras, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a critical concurrency bug within the AwaitRPC mechanism of the mailbox client, specifically a lost-wakeup race condition. By introducing an atomic operation for checking pending responses and registering waiters, the client now reliably handles RPC responses, preventing scenarios where notifications could be missed. This fix significantly improves the stability of the client, resolving a previously observed flaky test in CI.

Highlights

  • Atomic RPC Waiting: The AwaitRPC function was refactored to atomically check for pending responses and register waiters, eliminating a lost-wakeup race condition.
  • Flaky Test Fix: This change resolves a context deadline exceeded flake in TestClient_ConcurrentInFlightDoesNotDrop by addressing the underlying concurrency bug.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • mailbox/client/client.go
    • AwaitRPC was modified to use the new popPendingOrAddWaiter function for handling RPC responses.
    • The popPending and addWaiter functions were removed, as their functionality was consolidated.
    • A new popPendingOrAddWaiter function was introduced to atomically manage pending responses and waiter registration.
Activity
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@bhandras
bhandras force-pushed the fix/mailbox-awaitrpc-lost-wakeup branch from db828de to fb6c25b Compare February 19, 2026 20:46

@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

The changes effectively address the lost-wakeup flake in AwaitRPC by making the response check and waiter registration atomic. The introduction of popPendingOrAddWaiter correctly synchronizes access to pending responses and waiter channels, resolving the race condition. The code is clear and the fix is well-explained in the PR description.

TestClient_ConcurrentInFlightDoesNotDrop was flaking with "context
deadline exceeded" due to a lost-wakeup race in AwaitRPC.

AwaitRPC checked the pending-response map and registered a waiter in
two separate steps under different lock acquisitions. If
handleEnvelope cached a response between the pending check and waiter
registration, it notified only the existing waiters — the new waiter
was never signaled and blocked until its context timed out.

Fix this by introducing popPendingOrAddWaiter, which atomically
returns a pending response or registers a waiter, closing the
missed-notification window.
@bhandras
bhandras force-pushed the fix/mailbox-awaitrpc-lost-wakeup branch from fb6c25b to 32ac83b Compare February 19, 2026 21:02
@bhandras
bhandras requested a review from Roasbeef February 19, 2026 21:08
@Roasbeef

Copy link
Copy Markdown
Member

Folded into the latest series.

@Roasbeef Roasbeef closed this Feb 25, 2026
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