You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Issues with NoSynchronizationContextScope and Orleans #1130
Im try to integrate orleans with postgres. I use npgsql DbProvider for connect to db server.
I found that npgsql (since 3.0.0) not works inside orleans silo. I think that it happens because Orleans uses own task sheduler and thread pool.
Im also found how fix it fix 1 fix 2
But im not sure that is correct and dont understand why it work.
Im prepared test environment for reproduce this bug.
@zloom, it would be good to have more details than "doesn't work" - are you getting an exception? What is it it (and what is the stack trace)? How is Npgsql being used, can you submit a code sample which shows the issue?
The fixes you propose simply take async operations and make them sync, which is not a good thing. Can't you just use the sync APIs instead (e.g. Write instead of WriteAsync)?
It happens without exception, just wait infinite here.
Im prepared example here.
I cant use sync api because npgsql db queries executed inside orleans here
Npgsql integrated throug dbConnectionFactory, and all work fine until 3.0.0 version.
@zloom I'd need to see some minimal code sample which creates the issue in order to investigate it... Simply converting the async operations to sync isn't an option - if there's a bug in the async implementation somewhere it needs to be found and fixed...
@roji I stumbled on the same problem, and found solution. Because MS Orleans use it's own scheduler, NoSynchronizationContextScope not working and thread is deadlocked. When i add ConfigureAwait(false) to all methods, as it was before refactoring, it's worked like a charm. I can create PR if you want. Maybe there is a global solution but i can't find one.
@PDarkTemplar thanks for the info. I'm not 100% sure as to the possible interaction between Orlean's custom scheduler and NoSynchronizationContextScope.
Note that the original post mentioned issues in 3.0 as well, although NoSynchronizationContextScope was only introduced in Npgsql 3.2. Before 3.2 there were ConfigureAwait(false) - can you confirm that you're not encountering the issue with 3.1.10?
There was a reason we switched from ConfigureAwait(false) to NoSynchronizationContextScope - aside from the somewhat improved readability (not very important), there is an entire code path which needs to be executed twice, sometimes with a sync context and sometimes without; when big multi-statement (batched) commands are sent synchronously, Npgsql internally switches to async to avoid deadlocks with PostgreSQL, but runs the callbacks on a private internal thread - this is what the NoSynchronizationContextScope performs. In other words, if you were to switch things back to use ConfigureAwait(false), we'd have to duplicate non-trivial portions of the code path, once with ConfigureAwait(false) and once without.
Please try to confirm that the issue is with NoSynchronizationContextScope by testing on Npgsql 3.1.10, which doesn't use it yet.
@roji So i tried Npgsql 3.1.10 as you mentioned and all worked as expected. then i tried again latest, not modified assembly and it is deadlocked. Also here is logs from my orleans dev startup:
1) 3.1.10.txt
2) 3.2.2.txt
As you can see on 3.2.2 it is stacked on initializing connection to PostgeSQL(Don't pay attention to membership name): MembershipOracle starting on host = VULTURE address = S127.0.0.1:11111:228237983 at 2017-03-26 15:26:24.892 GMT, backOffMax = 00:00:20
Hmm, not sure what to look for in those logs - am not familiar at all with Orleans.
In any case, not sure what should be done here - I definitely don't want to duplicate an entire codepath just because of this... Will try to think about this for 3.3, stick to 3.1.10 as a workaround in the meantime.
If you (or anyone else) has any input or ideas please feel free to share them here.
I did a little research, so the problem is indeed with TaskScheduler. When you use ConfigureAwait(false) code ignores custom parent scheduler and grabs task from default pool, when returns from async operation. So i think about several options:
Write extension with wrapper on TaskCompletionSource, but i don't know anything about overhead or will it work
Version 3.2.4 includes a fix for #1593, which was a pretty critical bug with the way NoSynchronizationContext was implemented. Just to make sure this Orleans issue isn't related, can someone please test with 3.2.4 and report on whether the bug is still there?
Activity
roji commentedon May 26, 2016
@zloom, it would be good to have more details than "doesn't work" - are you getting an exception? What is it it (and what is the stack trace)? How is Npgsql being used, can you submit a code sample which shows the issue?
The fixes you propose simply take async operations and make them sync, which is not a good thing. Can't you just use the sync APIs instead (e.g. Write instead of WriteAsync)?
zloom commentedon May 26, 2016
It happens without exception, just wait infinite here.
Im prepared example here.
I cant use sync api because npgsql db queries executed inside orleans here
Npgsql integrated throug dbConnectionFactory, and all work fine until 3.0.0 version.
roji commentedon May 27, 2016
@zloom I'd need to see some minimal code sample which creates the issue in order to investigate it... Simply converting the async operations to sync isn't an option - if there's a bug in the async implementation somewhere it needs to be found and fixed...
zloom commentedon May 27, 2016
My code sample to reproduce error is https://github.com/zloom/npgsql/blob/dev/Npgsql.Orleans/Program.cs
I forked repository of npgsql and added project Npgsql.Orleans to solution.
PDarkTemplar commentedon Mar 24, 2017
@roji I stumbled on the same problem, and found solution. Because MS Orleans use it's own scheduler,
NoSynchronizationContextScopenot working and thread is deadlocked. When i addConfigureAwait(false)to all methods, as it was before refactoring, it's worked like a charm. I can create PR if you want. Maybe there is a global solution but i can't find one.roji commentedon Mar 26, 2017
@PDarkTemplar thanks for the info. I'm not 100% sure as to the possible interaction between Orlean's custom scheduler and NoSynchronizationContextScope.
Note that the original post mentioned issues in 3.0 as well, although NoSynchronizationContextScope was only introduced in Npgsql 3.2. Before 3.2 there were
ConfigureAwait(false)- can you confirm that you're not encountering the issue with 3.1.10?There was a reason we switched from
ConfigureAwait(false)to NoSynchronizationContextScope - aside from the somewhat improved readability (not very important), there is an entire code path which needs to be executed twice, sometimes with a sync context and sometimes without; when big multi-statement (batched) commands are sent synchronously, Npgsql internally switches to async to avoid deadlocks with PostgreSQL, but runs the callbacks on a private internal thread - this is what the NoSynchronizationContextScope performs. In other words, if you were to switch things back to useConfigureAwait(false), we'd have to duplicate non-trivial portions of the code path, once withConfigureAwait(false)and once without.Please try to confirm that the issue is with NoSynchronizationContextScope by testing on Npgsql 3.1.10, which doesn't use it yet.
PDarkTemplar commentedon Mar 26, 2017
@roji So i tried Npgsql 3.1.10 as you mentioned and all worked as expected. then i tried again latest, not modified assembly and it is deadlocked. Also here is logs from my orleans dev startup:
1)
3.1.10.txt
2)
3.2.2.txt
As you can see on 3.2.2 it is stacked on initializing connection to PostgeSQL(Don't pay attention to membership name):
MembershipOracle starting on host = VULTURE address = S127.0.0.1:11111:228237983 at 2017-03-26 15:26:24.892 GMT, backOffMax = 00:00:20roji commentedon Mar 26, 2017
Hmm, not sure what to look for in those logs - am not familiar at all with Orleans.
In any case, not sure what should be done here - I definitely don't want to duplicate an entire codepath just because of this... Will try to think about this for 3.3, stick to 3.1.10 as a workaround in the meantime.
If you (or anyone else) has any input or ideas please feel free to share them here.
[-]WriteBuffer error inside Orleans silo.[/-][+]Issues with NoSynchronizationContextScope and Orleans[/+]PDarkTemplar commentedon Mar 26, 2017
I did a little research, so the problem is indeed with
TaskScheduler. When you useConfigureAwait(false)code ignores custom parent scheduler and grabs task from default pool, when returns from async operation. So i think about several options:TaskCompletionSource, but i don't know anything about overhead or will it workHope i give you some ideas.
roji commentedon Jun 15, 2017
Version 3.2.4 includes a fix for #1593, which was a pretty critical bug with the way NoSynchronizationContext was implemented. Just to make sure this Orleans issue isn't related, can someone please test with 3.2.4 and report on whether the bug is still there?
14 remaining items
roji commentedon Aug 5, 2021
wingrime commentedon Aug 6, 2021
roji commentedon Aug 9, 2021
Based on offline discussion with @vonzshik, we're going to hold back on this for now and reevaluate in the future based on user demand.
Flag Postgresql as asynchronous
Flag PostgreSQL as asynchronous (#9705)
Flag PostgreSQL as asynchronous (dotnet#9705)
[Journaling] Auto-retire & purge state machines (#9684)