Fix for Django Channels issue #962

78 views
Skip to first unread message

joonhyu...@gmail.com

unread,
Feb 5, 2019, 7:15:55 AM2/5/19
to Django developers (Contributions to Django itself)
Hello,

This is my first post here. I'd like to discuss some ways of fixing the issue for Django Channels. Briefly speaking, any test for Channels written with ChannelsLiveServerTestCase freezes in MacOS.

As a Mac user, I wanted to find a way to write tests, so I spent nearly half a day debugging Channels, and finally got the answer. The thing was that Python asyncio library does not behave very well with multiprocessing.

To be specific, ChannelsLiveServerTestCase relies on DaphneProcess, which again relies on asyncio. Finally, asyncio is based on Kqueue in BSD-like OSes. Unfortunately, Kqueue is not inherited to a child process upon fork(), while the file descriptor table does. This causes a problem when the DaphneProcess of a child process tries to register event handlers for sockets, because the file descriptor is still opened but the Kqueue does not exist anymore. At this point, MacOS users see an error: bad file descriptor. (I've checked lsof of the file descriptor, and found that it points to /dev/null.)

In Unix-like systems, although the above problem does not occur, the file descriptor table is shared with its child, so forking while asyncio event loop is opened should be avoided if possible. I suggest three ways to get around this issue.

1. Close the forked loop and create a new event loop when DaphneProcess runs.
2. Modify multiprocessing-based design to threading-based design. In other words, run tests in single process, multi-thread environment.
3. Run tests in single process, single thread environment.

The easiest way is 1, which I already committed in my own fork, but this looks like a cheating, so I'm not very happy with this solution. Nevertheless it passes all tests of Channels and Daphne, and moreover Channels tutorial works fine with BSD-like OSes such as MacOS. (I've tested on both MacOS and Linux.) If you think this is fine, then I will be happy to send a PR.

Which one would be the most desirable design?

Carlton Gibson

unread,
Feb 5, 2019, 7:48:55 AM2/5/19
to Django developers (Contributions to Django itself)
Hi Joonhyung.

Ah, the hardest issue in the box. Good. 
TBH I haven't had a chance to even look at that one yet, and I'll need to pull out the Stevens & Rago when I do...
— anyone here with serious chops, please do feel free to input. 🙂

I think best if you comment on the issue. There's already a good discussion there. 

Also, there's really no harm in opening a PR to show the change. Even if it's not the right solution, it helps to have something to look at. 

Kind Regards,

Carlton
Reply all
Reply to author
Forward
0 new messages