Skip to content

Issues with NoSynchronizationContextScope and Orleans #1130

Closed

Description

@zloom

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.

Activity

  1. roji commented on May 26, 2016

    @roji
    Member

    @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)?

  2. zloom commented on May 26, 2016

    @zloom
    Author

    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.

  3. roji commented on May 27, 2016

    @roji
    Member

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

  4. zloom commented on May 27, 2016

    @zloom
    Author

    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.

  5. PDarkTemplar commented on Mar 24, 2017

    @PDarkTemplar

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

  6. roji commented on Mar 26, 2017

    @roji
    Member

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

  7. PDarkTemplar commented on Mar 26, 2017

    @PDarkTemplar

    @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

  8. roji commented on Mar 26, 2017

    @roji
    Member

    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.

  9. added this to the milestone on Mar 26, 2017
  10. changed the title [-]WriteBuffer error inside Orleans silo.[/-] [+]Issues with NoSynchronizationContextScope and Orleans[/+] on Mar 26, 2017
  11. PDarkTemplar commented on Mar 26, 2017

    @PDarkTemplar

    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:

    1. Write extension with wrapper on TaskCompletionSource, but i don't know anything about overhead or will it work
    2. I found example of changing TaskScheduler on the fly http://stackoverflow.com/a/17243675/7499272 but don't know is this a good idea
    3. Use Task.Run in wrapper but it is definitely bad idea and a lot of overhead

    Hope i give you some ideas.

  12. roji commented on Jun 15, 2017

    @roji
    Member

    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?

  13. 14 remaining items

  14. roji commented on Aug 5, 2021

    @roji
  15. wingrime commented on Aug 6, 2021

    @wingrime
  16. modified the milestones: 6.0.0, Backlog on Aug 9, 2021
  17. roji commented on Aug 9, 2021

    @roji
    Member

    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.

  18. modified the milestones: Backlog, 8.0.0 on Sep 26, 2023
  19. assigned and unassigned on Nov 21, 2023
  20. added theissue type on Jun 18, 2025
  21. added a commit that references this issue on Oct 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions