X Tutup
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Npgsql/NpgsqlError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,11 @@ internal NpgsqlError(ProtocolVersion protocolVersion, Stream stream)
_routine = PGUtil.ReadString(stream);
;
break;
default:
// Unknown error field; consume and discard.
PGUtil.ReadString(stream);
;
break;

}
}
Expand Down
46 changes: 43 additions & 3 deletions src/Npgsql/NpgsqlState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,9 @@ internal IEnumerable<IServerResponseObject> ProcessBackendResponsesEnum(NpgsqlCo
{
// Process commandTimeout behavior.

if ((context.Mediator.CommandTimeout > 0) &&
(!context.Socket.Poll(1000000*context.Mediator.CommandTimeout, SelectMode.SelectRead)))
{
if ((context.Mediator.CommandTimeout > 0) &&
(!CheckForContextSocketAvailability(context, SelectMode.SelectRead)))
{
// If timeout occurs when establishing the session with server then
// throw an exception instead of trying to cancel query. This helps to prevent loop as CancelRequest will also try to stablish a connection and sends commands.
if (!((this is NpgsqlStartupState || this is NpgsqlConnectedState || context.CancelRequestCalled)))
Expand Down Expand Up @@ -596,6 +596,46 @@ protected IEnumerable<IServerResponseObject> ProcessBackendResponses_Ver_2(Npgsq
}
}

/// <summary>
/// Checks for context socket availability.
/// Socket.Poll supports integer as microseconds parameter.
/// This limits the usable command timeout value
/// to 2,147 seconds: (2,147 x 1,000,000 less than max_int).
/// In order to bypass this limit, the availability of
/// the socket is checked in 2,147 seconds cycles
/// </summary>
/// <returns><c>true</c>, if for context socket availability was checked, <c>false</c> otherwise.</returns>
/// <param name="context">Context.</param>
/// <param name="selectMode">Select mode.</param>
internal bool CheckForContextSocketAvailability (NpgsqlConnector context, SelectMode selectMode)
{
/* Socket.Poll supports integer as microseconds parameter.
* This limits the usable command timeout value
* to 2,147 seconds: (2,147 x 1,000,000 < max_int).
*/
const int limitOfSeconds = 2147;

bool socketPoolResponse = false;

// Because the backend's statement_timeout parameter has been set to context.Mediator.CommandTimeout,
// we will give an extra 5 seconds because we'd prefer to receive a timeout error from PG
// than to be forced to start a new connection and send a cancel request.
// The result is that a timeout could take 5 seconds too long to occur, but if everything
// is healthy, that shouldn't happen.
int secondsToWait = context.Mediator.CommandTimeout + 5;

/* In order to bypass this limit, the availability of
* the socket is checked in 2,147 seconds cycles
*/
while ((secondsToWait > limitOfSeconds) && (!socketPoolResponse))
{
socketPoolResponse = context.Socket.Poll (1000000 * limitOfSeconds, selectMode);
secondsToWait -= limitOfSeconds;
}

return socketPoolResponse || context.Socket.Poll (1000000 * secondsToWait, selectMode);
}

private enum BackEndMessageCode
{
IO_ERROR = -1, // Connection broken. Mono returns -1 instead of throwing an exception as ms.net does.
Expand Down
X Tutup