jetty.project icon indicating copy to clipboard operation
jetty.project copied to clipboard

Sink.write, Source.demand and Source.read safely reentrant guarantees

Open scrat98 opened this issue 1 year ago • 2 comments

Jetty Version 12.0.8

Jetty Environment Any

Java Version Any

Question Are these guarantees actually provided? "Implementations guarantee that calls to this method are safely reentrant so that stack overflows are avoided in the case of mutual recursion between the execution of the Callback and a call to this method."

I'm asking because found that ContentCopier uses IteratingCallback and would like to double check that. Thanks!

scrat98 avatar May 06 '24 15:05 scrat98

@scrat98 IteratingCallback and SerializedInvoker are two key mechanisms implemented in Jetty to prevent deep recursion when callback methods again invoke a method. We use these extensively through the code base and do have tests to check we do not deeply recurse.

Typically these mechanism avoid a deep recursion as follows:

  • application calls a method like write(last,buffer,callback)
    • the write method calls succeeded on the callback
      • the callback again calls write(last,buffer,callback)
        • the write succeeds, but it is noticed that we are already in the scope of a callback, so succeeded is not called.
    • the call to succeeded returns and we notice that another operation has now succeeded, so the next succeeded callback is made at this level rather than at the recursed level.
      • the callback again calls write(last,buffer,callback)
        • the write succeeds, but it is noticed that we are already in the scope of a callback, so succeeded is not called.
    • etc.

gregw avatar May 07 '24 03:05 gregw

Can this be closed?

gregw avatar May 07 '24 06:05 gregw

Thanks for clarity!

scrat98 avatar May 07 '24 17:05 scrat98