Unexpected async behaviour?

2019-02-06 Thread dawkot
import asyncdispatch, asyncnet template asyncDo(x) = asyncCheck (proc {.async.} = x)() let s = newAsyncSocket() s.setSockOpt(OptReuseAddr, true) s.bindAddr Port 12345 s.listen let c1, c2 = newAsyncSocket() for c in [c1, c2]: asyncCheck c.connect

Re: Unexpected async behaviour?

2019-02-07 Thread dom96
Please try it without the `asyncDo` template, that's extraordinary enough to possibly be causing a compiler bug.

Re: Unexpected async behaviour?

2019-02-09 Thread dawkot
Right, this works as expected: import asyncdispatch, asyncnet let s = newAsyncSocket() s.setSockOpt(OptReuseAddr, true) s.bindAddr Port 12345 s.listen let c1, c2 = newAsyncSocket() for c in [c1, c2]: asyncCheck c.connect("localhost", Port 12345

Re: Unexpected async behaviour?

2019-02-12 Thread lemonboy
It's not a bug, it's a feature :) You probably missed [this](https://nim-lang.github.io/Nim/manual.html#closures-creating-closures-in-loops) part of the manual where it is explained that closures capture variable by reference. In your example the inner `serve` captures `c` by reference and, as

Re: Unexpected async behaviour?

2019-02-13 Thread dawkot
Thanks for help.

Re: Unexpected async behaviour?

2019-02-18 Thread cdunn2001
> In your example the inner serve captures c by reference and, as a result, > you're always listening on the last accepted connection. I do not understand. To me, it looks like `let c =` will bind a new `c`, so that the previous iteration's closure will retain a reference to the previous `c`, w

Re: Unexpected async behaviour?

2019-02-19 Thread lucian
not only it needs a copy but it seems to require protecting by closureScope. This is a bit of a hassle really, I'd personally avoid closures in loops. import asyncdispatch, asyncnet, strutils let s = newAsyncSocket() s.setSockOpt(OptReuseAddr, true) s.bindAddr Port 1