Repository navigation
Conversation
roji
left a comment
There was a problem hiding this comment.
Good catch!
But I think this is better handled by rolling back the read buffer position when the timeout is caught, rather than adding the new logic (see the DoReadMessage sync fast-path above, which similarly rolls back). Rolling back would mean 0 added runtime overhead in this very hot path, and would probably also be easier to follow (less state variables in the method itself).
What do you think?
| return; // Multiplexing, cancellation | ||
|
|
||
| #if NET461 | ||
| Assert.Ignore("Async timeouts aren't supported with .net framework"); |
There was a problem hiding this comment.
nit: Assert.Ignore emits a "skip" message in the tests - we should only do this if the test is temporarily skipped, and we want to get back to it and fix it (eventually). Cases which will never work such as this one should simply return, making the test pass successfully.
There was a problem hiding this comment.
I think it would be better to just completely ignore this test for .net framework.
There was a problem hiding this comment.
Sure, conditional compilation is also fine. The only point here is not to have a "test skipped" message every time we run the tests, for something that will never be implemented.
| } | ||
|
|
||
| [Test, Description("Times out an async operation, testing that cancellation occurs successfully")] | ||
| public async Task ReaderTimeoutAsyncSoft() |
There was a problem hiding this comment.
I think this test would be simpler (and possibly a bit more reliable) with the new PG mock, what do you think?
There was a problem hiding this comment.
I see your point - but PG mock isn't here yet 😢
There was a problem hiding this comment.
Let me know if you have any issues implementing with the mock, the infrastructure is very new and may need to change a bit to accommodate new stuff.
Maybe I'm missing something, but should't it be possible for a case, when the first |
Can you explain a bit more by what you mean (maybe give a step-by-step example)? Ensure may fill the buffer, but should never move the ReadPosition. So if an Ensure times out, we should always be fine to move the position back 5 (code+length) and restart the loop at the same point. |
Here: npgsql/src/Npgsql/NpgsqlReadBuffer.cs Lines 149 to 158 in e34709d From what I understand, if the buffer is full (or doesn't have enough space), we essentially reset it. |
|
Ah yes, you're absolutely right... thanks. I still think the logic here is getting a bit complex. One possibly radical idea, is to move the cancellation down into the read buffer, into Ensures. This would mean we no longer use the loop inside ReadMessageLong to retry reading after cancellation is requested - that would be done inside Ensure's loop instead. One consequence would be to apply the cancellation-on-timeout logic more universally to anything happening in Npgsql (e.g. COPY), which may be another reason to do it. We'd have to be careful to reset the timeout back from CancellationTimeout to CommandTimeout when leaving Ensures. Another approach could be to "peek" at the code and length above (without changing the position), and only move the position forward after the I/O completed successfully. Let me know what you think. PS This made me realize that we need to make sure we don't cancel for multiplexing... |
Well, you're suggesting to move the complexity from the
Not sure how this should work, as it might take multiple reads until we've read all of the requested data. I do not see how it would help, if we fail in the middle of the reads.
Why not? I have not touched multiplexing at all, so more info would be appreciated. |
|
Sorry for abandoning this too, am back to it now.
The point of this thought, was that if handling of cancellation/timeout is an internal concern of the buffer, the connector code in ReadMessageLong no longer has to track whether a timeout has already occurred (in a separate variable) - Ensures would be guaranteed to either succeed, or through a definitive timeout/cancellation exception that doesn't require us to retry again. This would naturally make this bug go away. We've basically overloaded the ReadMessageLong loop to take care of cancellation/timeout retries, and now we're adding a check to know whether we're in the first or second loop. Aside from the code complication (it's getting difficult to follow), that's a bit more state to update, which also means some perf (though probably negligible).
The idea here, is to replace the ReadByte (code) and ReadInt32 (length) calls with just peeking into the buffer - the key point is to not advance the ReadPosition. If we do this, those 5 bytes will still be there even if a timeout happens etc. It would be rather hacky though (e.g. PeekCodeAndLength on read buffer which returns a tuple). |
|
One more comment...
On the one hand, I tend to agree and that was the original idea. But on the other hand, breaking the connection is a good counter-example - it's something universal the should happen for any read (regardless of who asks for it), so it makes sense to push this down into the single point where I/O occurs. Otherwise, we'd have to remember to break-on-exception wherever Ensure gets called, which is a lot of places. This leads to a more important point. I don't really care that much where the break or cancellation/timeout logic lives - on the buffer, on the connector or on a 3rd new type. However, the read buffer gets used directly by all our type handlers, and I think we want to trigger the same soft and hard cancellation flow as we have in NpgsqlConnector.ReadMessageLong, no? If so, then unless we want to change all type handlers, it seems like the only way is to push that logic down into the buffer. |
|
Let's go with the 'cancellation in the buffer' decision. Peak one seams to me brings too many problems (just imagine a cycle, where we have to read 10 times from the buffer - that is, 9 peaks and 1 ensure). |
|
OK. For the peeking, the point was simply to peek 5 bytes, and only advance the buffer's read position later,, after we know we have completed I/O for the message; so I don't think there's any particular complications. But in any case I'm more worried about making the cancellation/timeout logic universal with everyone touching the buffer (COPY, handlers...), so I think it's the right choice - unless you have a better idea. |
Which is fine. Well, as long as there is only one place, where this bug can happen. I would rather fix this permanently.
No, no ideas. As long as sending the cancellation request from a handler doesn't break anything, it should be fine. |
|
Closing, as it's merged with #3191 |
Closes #3212