gRPC C++ server streaming reactor: Is it possible to sync StartWrite with OnDone

68 views
Skip to first unread message

John Smith

unread,
Feb 19, 2026, 10:21:14 AMFeb 19
to grpc.io
Hi, in my scenario, I need to initiate writing to the stream from my application thread when the data is ready. It is unclear what to do in situations that lead to the RPC closing and the OnDone call at the end.

> Thread safety: the above calls may take place concurrently, except that OnDone will always take place after all other reactions. No further RPC operations are permitted to be issued after OnDone is invoked.

It is possible to organize the code so that StartWrite calls are only from the grpc threads, but in this case there is no way to tell the reactor to check for new data to send.
Mark D. Roth said that it is possible to call StartWrite from a user thread (thank you very much for your help, Mark):
There's no requirement that `StartWrite()` can be called only from a reactor thread -- you can call that from anywhere, as long as you have proper synchronization. The only requirement is that there can be only one write pending at a time.

But how to synchronize the StartWrite call from the user thread with the OnDone call  from the reactor thread ? Is this even possible?

P.S. experiments show that if StartWrite is called after entering OnDone and we wait for OnWriteDone to be called while we are in OnDone, it is still UB, since OnWriteDone will be followed by MayBeDone 


John Smith

unread,
Feb 24, 2026, 11:46:14 AMFeb 24
to grpc.io
RTFM, https://grpc.io/docs/languages/cpp/best_practices 
FAQ 5 and 6

  • When is OnDone called on the server?

    All reactions must finish running (including OnCancel when relevant) and server must have called Finish.

  • What is “within-reactor flow” vs. “outside-reactor flow” and why does it matter?

    Within-reactor flow is when operations are started from inside reactions (or the reactor constructor) such as OnWriteDone starting another write. These make sense since there can only be one read and one write in flight and starting them from the reaction can help maintain that.

    Outside-reactor flow is when operations on the stream are started from outside reactions. This makes sense since reactions aren’t supposed to block and the application may not be ready to perform its next read or write. Note that outside-reactor flows require the use of holds to synchronize on the client side. Server side uses Finish to synchronize outside-reactor calls; the application should not start more operations after calling Finish.


четверг, 19 февраля 2026 г. в 18:21:14 UTC+3, John Smith:
Reply all
Reply to author
Forward
0 new messages