Repository navigation
Description
While working on the state refactoring in #326, I thought a little about how Npgsql happens exceptions and how they interact with the connector/connection state.
If any sort if error occurs (networking, server-reported), Npgsql wraps it in an NpgsqlException. It seems that it is currently impossible to know what the state of the connection is following an exception; the user has no means of knowing whether they can continue using the connection after an exception (they can after a server-reported exception, e.g. invalid SQL, but they can't after a networking exception).
What we probably need to do is to update the connector (and connection) state appropriately. A fatal exception (e.g. networking) should set the connection state to Broken, signaling to the user that they need to open and close if they wish to continue (this is described here, even if the ConnectionState.Broken is still marked as reserved for future use). This seems pretty straightforward to me, let me know if you see any issue.
One additional thing I'm less sure about, is the handling of ThreadAbortException in various places in the codebase - we attempt to cancel the query and close. Thread.Abort() in general is considered to be an extremely bad way of stopping user tasks (see this article, and also this stackoverflow discussions and especially Eric Lippert's answer).
The put it very simply, if some user does Thread.Abort() at the wrong moment, it's very easy to totally mess up Npgsql's internal state in a very bad way, since the ThreadAbortException can occur at any point in the code. So in general this kind of scenario isn't handled, especially by 3rd-party libraries, except for very specific scenarios.
I'm leaning towards removing special handling of Thread.Abort(), and allow regular exception handling to take place - change the connection state to Broken. We'd lose the automatic cancellation that currently happens when a thread is aborted during a query, but I think it's safer/healthier/cleaner to say that Thread.Abort() is simply not supported (since it pretty much can't be, in any guaranteed and meaningful way).
Can anyone with experience on this comment?
Activity
jbcooley commentedon Sep 2, 2014
I want to say that this came up in the context of IIS cancelling requests. I believe the connection pool was getting trashed when requests were cancelled. Of course it's been a long time, so I may be wrong on the details.
roji commentedon Sep 4, 2014
Thanks for the comment @jbcooley.
Looking at it some more:
But I admit I don't know enough about IIS, it would be great to have an expert opinion here. I'm still heavily leaning towards removing special handling of ThreadAbortedException, but don't think we should do it before we get a clear picture.
roji commentedon Sep 12, 2014
An unrelated question. I have a test failing on the build server because Npgsql generated an IOException and the test expected an NpgsqlException.
I have no idea why this happened now, but there's a general question: should Npgsql always generate NpgsqlException, and wrap causes as inner exceptions? Personally I prefer the idea of always having NpgsqlExceptions, this way users can know by the exception type that it came from Npgsql. But this does not seem to be the behavior in SQL Server:
It will also be more difficult to hunt down and wrap exceptions like IOExceptions. Easier to just let them bubble up to the user.
Any thoughts/comments?
Emill commentedon Sep 12, 2014
I also think it's better to separate database errors from things like network errors...
franciscojunior commentedon Sep 13, 2014
This specific error is because it is trying to find the resources which contain the error message and it is not being able to be found. So it throws the IOException because it is being generated the the file reader.
That documentation was exactly the inspiration for the current situation of NpgsqlException: All server and network related exceptions are wrapped with NpgsqlException and all other client side, like invalid argument, and others are thrown as standard common language.
roji commentedon Sep 14, 2014
Sorry, I think I posted the link to the wrong failing test. This is the right one. I will investigate what's going on (only happens on build server, not here). The stack trace is:
But regardless, @franciscojunior, aren't network-related exceptions a cilent-side issue, and therefore should not be wrapped by NpgsqlException? According to my understanding of the SQL Server spec only errors returned from the backend (i.e. a backend ErrorResponse message) should be an Sql/NpgsqlException. What do you think?
franciscojunior commentedon Sep 15, 2014
It seems the link is still pointing to the resource file missing exception :)
This is a case where we aren't honoring the idea of wrapping the server side exception with NpgsqlException. This is an error in Npgsql. It should be wrapping this IOException in a NpgsqlException.
Hmmmm, my understanding was that network related exceptions were server side related. But I see what you are saying. As the network exception is derived from a client side interaction, instead of a server processing, it should be thrown to the client as the original exception. And re reading sqlException and SqlCommand.ExecuteReader() method descriptions, I think you are right and the current Npgsql approach of wrapping all network and server related exceptions inside NpgsqlException is not ideal.
I hope it is not too late to change this assumption. As 3.0 is introducing a lot of potential breakage, I think this is the right time to correct this :)
+1 for changing this.
So, in this new interpretation, the error above is not an error. It is the desired behavior and the test should be changed to reflect that.
Use NpgsqlException for backend errors only
roji commentedon Mar 29, 2015
AFAIK all exception-related issues are solved. There's still special thread-abort exception handling in the connection pool, we'll look at this in 3.1 when the rest of the connection pool will take place.