Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unclear closure behavior in asynchronous and parallel tasks #23130

Open
appascoe opened this issue Aug 4, 2017 · 2 comments
Open

Unclear closure behavior in asynchronous and parallel tasks #23130

appascoe opened this issue Aug 4, 2017 · 2 comments
Labels
doc This change adds or pertains to documentation

Comments

@appascoe
Copy link

appascoe commented Aug 4, 2017

Discourse thread for reference: https://discourse.julialang.org/t/inconsistent-results-based-on-number-of-processes/5191

At the very least, there's a documentation issue. The example of a network server that can service multiple connections has a critical flaw because, we believe, the changes in closures from 0.4 to 0.5:

server = listen(2000)
while true
    sock = accept(server)
    @async begin
        x = readline(sock)
        write(sock, x)
    end
end
In [1]: import socket

In [2]: s = socket.socket()

In [3]: t = socket.socket()

In [4]: s.connect(('', 2000))

In [5]: t.connect(('', 2000))

In [6]: s.send('check 1\n')
Out[6]: 8

In [7]: t.recv(1024)
Out[7]: 'check 1'

Because a second connection was opened before there was a write to the first connection, sock got overwritten, causing the write on the first connection to be echoed on the second. This makes asynchronous and parallel tasks more difficult to reason about, and it's not obvious that this is expected/desired behavior.

@vtjnash
Copy link
Sponsor Member

vtjnash commented Aug 4, 2017

Yes, that example would be better written as

server = listen(2000)
while true
    let sock = accept(server)
        @async begin
            x = readline(sock)
            write(sock, x)
        end
    end
end

@vtjnash vtjnash added the doc This change adds or pertains to documentation label Aug 4, 2017
@appascoe
Copy link
Author

appascoe commented Aug 4, 2017

The practice of enforcing closures should probably also be a section in the Parallel Computing chapter as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc This change adds or pertains to documentation
Projects
None yet
Development

No branches or pull requests

2 participants