Skip to content

Buffer re-read on a timeout fix - #3213

Closed
vonzshik wants to merge 5 commits into
npgsql:mainfrom
vonzshik:reader-read-timeout-fix
Closed

vonzshik wants to merge 5 commits into
npgsql:mainfrom
vonzshik:reader-read-timeout-fix

Conversation

@vonzshik

@vonzshik vonzshik commented Oct 4, 2020

Copy link
Copy Markdown
Contributor

Closes #3212

@roji roji left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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?

Comment thread test/Npgsql.Tests/ReaderTests.cs Outdated
return; // Multiplexing, cancellation

#if NET461
Assert.Ignore("Async timeouts aren't supported with .net framework");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think it would be better to just completely ignore this test for .net framework.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this test would be simpler (and possibly a bit more reliable) with the new PG mock, what do you think?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I see your point - but PG mock isn't here yet 😢

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It is now 😄

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

@vonzshik

vonzshik commented Oct 5, 2020

Copy link
Copy Markdown
Contributor Author

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).

Maybe I'm missing something, but should't it be possible for a case, when the first Ensure would fill the buffer like [000..12345], and the second Ensure would move ReadPosition to 0?

@roji

roji commented Oct 5, 2020

Copy link
Copy Markdown
Member

Maybe I'm missing something, but should't it be possible for a case, when the first Ensure would fill the buffer like [000..12345], and the second Ensure would move ReadPosition to 0?

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.

@vonzshik

vonzshik commented Oct 5, 2020

Copy link
Copy Markdown
Contributor Author

Maybe I'm missing something, but should't it be possible for a case, when the first Ensure would fill the buffer like [000..12345], and the second Ensure would move ReadPosition to 0?

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:

if (ReadPosition == FilledBytes)
{
Clear();
}
else if (count > Size - FilledBytes)
{
Array.Copy(Buffer, ReadPosition, Buffer, 0, ReadBytesLeft);
FilledBytes = ReadBytesLeft;
ReadPosition = 0;
}

From what I understand, if the buffer is full (or doesn't have enough space), we essentially reset it.

@roji

roji commented Oct 5, 2020

Copy link
Copy Markdown
Member

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...

@vonzshik

vonzshik commented Oct 5, 2020

Copy link
Copy Markdown
Contributor Author

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.

Well, you're suggesting to move the complexity from the Connector to the ReadBuffer. Doesn't look like much of a difference for me 😁.
All jokes aside, as much as I don't like this, I do not see any other way to deal with this problem.
From a design standpoint, ReadBuffer shouldn't care about cancellation - it should only deal with I/O. Also, in my mind, ReadBuffer should never break the connection - that is the Connector's problem.
Essentially, ReadBuffer should only do what its name is suggesting - read in the buffer. But changes such as this would take too much time for little to no effect.
And as you've said, it's and ideal place to request the cancellation on a timeout.

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.

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.

PS This made me realize that we need to make sure we don't cancel for multiplexing...

Why not? I have not touched multiplexing at all, so more info would be appreciated.

@roji

roji commented Oct 14, 2020

Copy link
Copy Markdown
Member

Sorry for abandoning this too, am back to it now.

Well, you're suggesting to move the complexity from the Connector to the ReadBuffer. Doesn't look like much of a difference for me grin.

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).

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.

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.

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).

@roji

roji commented Oct 14, 2020

Copy link
Copy Markdown
Member

@vonzshik maybe the best way forward with this and #3191, is to move ahead with your approach, and then I'd try to play around and see what I come up with in a later PR which you'd review - let me know.

@roji

roji commented Oct 14, 2020 •

Copy link
Copy Markdown
Member

One more comment...

From a design standpoint, ReadBuffer shouldn't care about cancellation - it should only deal with I/O.
Also, in my mind, ReadBuffer should never break the connection - that is the Connector's problem.

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.

@vonzshik

Copy link
Copy Markdown
Contributor Author

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).

@roji

roji commented Oct 14, 2020

Copy link
Copy Markdown
Member

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.

@vonzshik

vonzshik commented Oct 14, 2020 •

Copy link
Copy Markdown
Contributor Author

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.

Which is fine. Well, as long as there is only one place, where this bug can happen. I would rather fix this permanently.

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.

No, no ideas. As long as sending the cancellation request from a handler doesn't break anything, it should be fine.

@vonzshik

Copy link
Copy Markdown
Contributor Author

Closing, as it's merged with #3191

@vonzshik vonzshik closed this Oct 17, 2020
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.

A timeout while calling NpgsqlReader.Read can re-read from the buffer

3 participants