Skip to content

investigate kqueue processing on FreeBSD #26633

Closed

Description

@wfurt

This is primarily about named pipes but the problem may not be specific to it.
msbuild and Roslyn use named pipes to implement interprocess communication.
They randomly hang when running on FreeBSD. Also tests from System.IO.Pipes/tests/NamedPipeTests are failing on FreeBSD.

truss -o /tmp/trace.txt -f -d -H ../../../testhost/netcoreapp-FreeBSD-Debug-x64/dotnet xunit.console.netcore.exe System.IO.Pipes.Tests.dll -xml testResults.xml -notrait Benchmark=true -notrait category=nonnetcoreapptests -notrait category=nonfreebsdtests -notrait category=OuterLoop -notrait category=failing -showprogress -parallel none -method System.IO.Pipes.Tests.NamedPipeTest_Simple_ServerInOut_ClientOut.ManyConcurrentOperations

grep -e open -e kevent -e send -e connect -e accept -e recv /tmp/trace.txt

33324 100296: 1.389680761 connect(100,{ AF_UNIX "/tmp/CoreFxPipe_xzlvd23f.e50" },31) = 0 (0x0)
....
33324 101022: 1.491534421 sendmsg(0x64,0x7fffde7af760,0x0) = 512 (0x200)
33324 101022: 1.491692207 sendmsg(0x64,0x7fffde7af760,0x0) = 512 (0x200)
33324 101022: 1.492027215 sendmsg(0x64,0x7fffde7af760,0x0) ERR#35 'Resource temporarily unavailable'
33324 100288: 1.492083817 kevent(97,0x0,0,{ 103,EVFILT_READ,EV_CLEAR|EV_RECEIPT,0x0,0x2000,0x1 },32,0x0) = 1 (0x1)
33324 100288: 1.495819290 kevent(97,0x0,0,{ 100,EVFILT_READ,EV_CLEAR|EV_RECEIPT|EV_EOF,0x0,0x0,0x2 },32,0x0) = 1 (0x1)
33324 101022: 1.495844505 kevent(97,{ 100,EVFILT_READ,EV_ADD|EV_CLEAR|EV_RECEIPT,0x0,0x0,0x2 100,EVFILT_WRITE,EV_ADD|EV_CLEAR|EV_RECEIPT,0x0,0x0,0x2 },2,0x0,0,0x0) = 0 (0x0)
33324 100288: 31.737951300 kevent(97,0x0,0,{ 103,EVFILT_READ,EV_CLEAR|EV_RECEIPT|EV_EOF,0x0,0x0,0x1 },32,0x0) = 1 (0x1)
33324 100617: 1.588647366 recvmsg(0x67,0x7fffde9b12d0,0x0) = 512 (0x200)
33324 101060: 1.590081821 recvmsg(0x67,0x7fffddfac2d0,0x0) = 512 (0x200)
33324 101036: 1.592974978 recvmsg(0x67,0x7fffde5af2d0,0x0) = 512 (0x200)
33324 101285: 1.594100823 recvmsg(0x67,0x7fffdddab2d0,0x0) = 512 (0x200)
33324 101041: 1.594549209 recvmsg(0x67,0x7fffde1ad2d0,0x0) = 512 (0x200)
33324 101286: 1.595597996 recvmsg(0x67,0x7fffddbaa2d0,0x0) = 512 (0x200)
33324 101287: 1.595811634 recvmsg(0x67,0x7fffdd9a92d0,0x0) = 512 (0x200)
33324 100296: 1.595941461 recvmsg(0x67,0x7fffdebf32d0,0x0) = 512 (0x200)
33324 101038: 1.596414729 recvmsg(0x67,0x7fffde3ae2d0,0x0) = 512 (0x200)
33324 101036: 1.599841005 recvmsg(0x67,0x7fffde5af2d0,0x0) = 512 (0x200)
33324 101038: 1.601945788 recvmsg(0x67,0x7fffde3ae2d0,0x0) = 512 (0x200)
33324 100617: 1.605427075 recvmsg(0x67,0x7fffde9b12d0,0x0) = 512 (0x200)
33324 101038: 1.610128113 recvmsg(0x67,0x7fffde3ae2d0,0x0) = 512 (0x200)
33324 100617: 1.613307804 recvmsg(0x67,0x7fffde9b12d0,0x0) = 512 (0x200)
33324 101038: 1.616360676 recvmsg(0x67,0x7fffde3ae2d0,0x0) = 512 (0x200)
33324 101060: 1.617765858 recvmsg(0x67,0x7fffddfac2d0,0x0) = 512 (0x200)
33324 101285: 1.619783806 recvmsg(0x67,0x7fffdddab2d0,0x0) ERR#35 'Resource temporarily unavailable'
33324 100288: 31.737951300 kevent(97,0x0,0,{ 103,EVFILT_READ,EV_CLEAR|EV_RECEIPT|EV_EOF,0x0,0x0,0x1 },32,0x0) = 1 (0x1)

when send() fails we subscribe for readable and writable events. The reader will read data and empty socket buffers. But the writeable event will never come. Test will fail on timeout after 30s, msbuild just hangs forever.

Activity

  1. added a commit that references this issue on Jun 27, 2018
  2. radovanovic commented on Jun 27, 2018

    @radovanovic

    Hi Tomas, can you attach entire output of truss (I didn't really check much of corefx code, but I did lot of unix domain socket programming lately, so maybe I can spot something)?

  3. wfurt commented on Jun 27, 2018

    @wfurt
    MemberAuthor
  4. radovanovic commented on Jun 27, 2018

    @radovanovic

    To me it looks most likely that there is something broken with behavior of kevent EVFILT_READ on already half-closed socket - there was call

    33324 100296: 1.393415335 shutdown(100,SHUT_RD)  = 0 (0x0)
    ...
    33324 100288: 1.495819290 kevent(97,0x0,0,{ 100,EVFILT_READ,EV_CLEAR|EV_RECEIPT|EV_EOF,0x0,0x0,0x2 },32,0x0) = 1 (0x1)
    33324 101022: 1.495844505 kevent(97,{ 100,EVFILT_READ,EV_ADD|EV_CLEAR|EV_RECEIPT,0x0,0x0,0x2 100,EVFILT_WRITE,EV_ADD|EV_CLEAR|EV_RECEIPT,0x0,0x0,0x2 },2,0x0,0,0x0) = 0 (0x0)
    

    I would guess that order of last two is actually reversed (first subscribing from one thread and immediate return in second thread, possibly before kevent in first thread returned).
    I really can't see anything else suspicious. I will try to write simple test in C for that exact situation to see what will happen

  5. wfurt commented on Jun 27, 2018

    @wfurt
    MemberAuthor

    yes, I think that can be ordering from different threads. Also note that msbuild and Roslyn do not shutdown the socket and they keep it bidirectional. I think key part is trying to register for both readable and writable event at the same time. If you look at my change, I simply do two kevent() calls and avoids the problem for some reason.

  6. radovanovic commented on Jun 27, 2018

    @radovanovic

    Although probably doesn't make much sense either to check if socket that was closed from the same process has become readable :-)

  7. wfurt commented on Jun 27, 2018

    @wfurt
    MemberAuthor

    As far as I know the readable subscription is there to pick up socket errors. I don't think you would get writable event of socket disconnects. If you have access to Mac it would be great to run tests there are well to see if kqueue on FreeBSD behaves different than osx.

  8. radovanovic commented on Jun 27, 2018

    @radovanovic

    What I suspect is happening is that when you put both in one call, readable detects that there is eof and it returns but blocks other filters (I would also guess that if you first set writable filter and then readable filter you will get same broken behavior, but if you first register readable and then writable that would make it work).
    Anyway simple test case in C will probably show if our guess is correct (I will try to write it tomorrow).

  9. radovanovic commented on Jun 27, 2018

    @radovanovic

    Regarding kevent behavior OSX vs FreeBSD - it definitely behaves differently (I had to rewrite mono's version of FS watcher long time ago - it was for OSX and it didn't work at all on FreeBSD).

  10. radovanovic commented on Jun 29, 2018

    @radovanovic

    Seems like bug in kevent implementation in FreeBSD, exactly same behavior from C (it alerts only about first filter if more than one passed in single kevent call). Your workaround with multiple calls to kevent seems to work no matter how you order it. Simple test is attached (expected result would be to see "XXX received EVFILT_WRITE" for any XXX, but instead for BothReadWrite only EVFILT_READ is received), We should file bug report
    kevent_test.c.gz

  11. wfurt commented on Jul 6, 2018

    @wfurt
    MemberAuthor

    Thanks for the update @radovanovic. Do you know if this is specific to Unix Domain Sockets or is it general problem? For now, I submitted the workaround but it would be nice to have cmake detection at some point as suggested in linked PR.

    Also if you have experience with fs watcher, please add notes to #14716 (or get it done :) )

  12. radovanovic commented on Jul 12, 2018

    @radovanovic

    Sorry for being slow (too much work). I don't know, I did lot of socket programming but I didn't do lot of kqueue programming (in watcher I was using EVFILT_NODE, which was only one structure to add). I will fill that bug report since nobody came back on mailing list.
    Regarding watcher I would really be glad to help, but at the moment you have all this very complicated (and I don't have access to either Windows or Linux machine), best I can do is look how you did Windows and Linux watcher and adjust my watcher to the same design, but somebody else will have to test it and try to build it.

  13. wfurt commented on Jul 12, 2018

    @wfurt
    MemberAuthor

    full FreeBSD build is very close @radovanovic. With that, you will be able to compile code and run test on single FreeBSD system.

  14. radovanovic commented on Jul 12, 2018

    @radovanovic

    https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=229741 (although it shouldn't matter for implementation since you have already found workaround)

  15. 9 remaining items

  16. dotnet-policy-service commented on Dec 26, 2024

    @dotnet-policy-service
    Contributor

    Due to lack of recent activity, this issue has been marked as a candidate for backlog cleanup. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will undo this process.

    This process is part of our issue cleanup automation.

  17. added
    backlog-cleanup-candidate
    on Dec 26, 2024
  18. dotnet-policy-service commented on Jan 9, 2025

    @dotnet-policy-service
    Contributor

    This issue will now be closed since it had been marked no-recent-activity but received no further activity in the past 14 days. It is still possible to reopen or comment on the issue, but please note that the issue will be locked if it remains inactive for another 30 days.

  19. removed this from the Future milestone on Jan 9, 2025
  20. locked and limited conversation to collaborators on Feb 8, 2025
  21. removed
    backlog-cleanup-candidate
    on Feb 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-Metabughelp wantedos-freebsd

    Type

    No type

    Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions