Closed Aaronmsv closed 1 year ago
Hi Aaronmsv.
session.RunAsync returns a IResultCursor which represents the results stream. The error will only surface/throw when you fetch from that stream. In your test the correct thing to do is consume the result cursor.
var resultCursor = await session.RunAsync(query, parameters); resultCursor.Consume();
I suspect crash you are seeing currently is occuring when the result cursor returned by RunAsync goes out of scope and is disposed, perhaps in conflict of the session also being disposed at the same time.
When using ExecuteWriteAsync, the disposal of the result cursor happens when the lambda finishes thus taking place at the correct time and raising the exception you expect.
Ah I see. So in general, when using session.RunAsync
, you are responsible for the result cursor; and when you use ExecuteWriteAsync
, then it handles the result cursor for you (although both methods return IResultCursor
)?
Is it best to always do resultCursor.Consume();
?
I wouldn't describe it as ExecuteWriteAsync is handling the cursor for you, it is just scoped differently as it is with in the callback lambda that you supply. We consider it best practice to call Consume when finished using a result cursor, even in the ExecuteRead/ExecuteWrite methods.
I'll close the issue. Please feel free to re-open if needed.
Hi,
I've come across some weird exception handling when using
RunAsync
directly on a session. I was writing an integration test that expected aClientException
because of unique constraint and that's where I encountered the issue. There's no exception coming fromRunAsync
, but the test does crash later on. I would guess that the query is executed on another thread, which causes the exception to throw on another thread instead of the calling thread, or something similar.Since it was just a test, I would use
RunAsync
directly on a session instead of callingExecuteWriteAsync
and then using theIAsyncQueryRunner
. I didn't need e.g. automatic retry in the test.Environment information
Neo4j Version: 5.1.0 Community Neo4j Mode: Single instance Driver version: .NET driver 5.1.0 and 5.2.0 Operating System: Linux 6.0.9-arch1-1 x86_64
Steps to reproduce
I've included an xUnit test below that demonstrates the problem. I explicitly use try/catch here, instead of
Assert.ThrowsAsync
, to reduce the number of potentially contributing factors.I have omitted the code for
CreateSession()
, as it is an implementation detail on how it resolves the config and driver instance, but it's basically something like this:Expected behavior
The first catch block gets executed.
Actual behavior
The test crashes:
Workaround
In the test, replace the following line:
With: