{"action":"created","issue":{"url":"https://api.github.com/repos/nim-lang/Nim/issues/3518","labels_url":"https://api.github.com/repos/nim-lang/Nim/issues/3518/labels{/name}","comments_url":"https://api.github.com/repos/nim-lang/Nim/issues/3518/comments","events_url":"https://api.github.com/repos/nim-lang/Nim/issues/3518/events","html_url":"https://github.com/nim-lang/Nim/issues/3518","id":115623003,"number":3518,"title":"Async or socket bug","user":{"login":"ephja","id":1609261,"avatar_url":"https://avatars.githubusercontent.com/u/1609261?v=3","gravatar_id":"","url":"https://api.github.com/users/ephja","html_url":"https://github.com/ephja","followers_url":"https://api.github.com/users/ephja/followers","following_url":"https://api.github.com/users/ephja/following{/other_user}","gists_url":"https://api.github.com/users/ephja/gists{/gist_id}","starred_url":"https://api.github.com/users/ephja/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ephja/subscriptions","organizations_url":"https://api.github.com/users/ephja/orgs","repos_url":"https://api.github.com/users/ephja/repos","events_url":"https://api.github.com/users/ephja/events{/privacy}","received_events_url":"https://api.github.com/users/ephja/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/nim-lang/Nim/labels/Stdlib","name":"Stdlib","color":"eb6420"}],"state":"open","locked":false,"assignee":{"login":"dom96","id":246651,"avatar_url":"https://avatars.githubusercontent.com/u/246651?v=3","gravatar_id":"","url":"https://api.github.com/users/dom96","html_url":"https://github.com/dom96","followers_url":"https://api.github.com/users/dom96/followers","following_url":"https://api.github.com/users/dom96/following{/other_user}","gists_url":"https://api.github.com/users/dom96/gists{/gist_id}","starred_url":"https://api.github.com/users/dom96/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dom96/subscriptions","organizations_url":"https://api.github.com/users/dom96/orgs","repos_url":"https://api.github.com/users/dom96/repos","events_url":"https://api.github.com/users/dom96/events{/privacy}","received_events_url":"https://api.github.com/users/dom96/received_events","type":"User","site_admin":false},"milestone":null,"comments":1,"created_at":"2015-11-07T01:32:58Z","updated_at":"2015-11-19T09:46:45Z","closed_at":null,"body":"An assertion in acceptAddr fails when I run this code and then connect a single client\r\n```nim\r\nimport asyncnet, asyncdispatch\r\n\r\nvar clients {.threadvar.}: seq[AsyncSocket]\r\n\r\nproc processClient(client: AsyncSocket) =\r\n    echo \"DEBUG: process client\"\r\n    # stuff ..\r\n\r\nproc acceptClient(server: AsyncSocket) =\r\n    let clientFut = server.accept()\r\n    waitFor sleepAsync(100) or clientFut\r\n    if clientFut.finished:\r\n        echo \"DEBUG: client accepted\"\r\n        clients.add clientFut.read\r\n\r\nproc processClients(server: AsyncSocket) =\r\n    for client in clients:\r\n        processClient(client)\r\n\r\nproc main =\r\n    clients = @[]\r\n    var server = newAsyncSocket()\r\n    server.bindAddr(12345.Port)\r\n    server.listen()\r\n\r\n    while true:\r\n        acceptClient(server)\r\n        processClients(server)\r\n\r\nmain()\r\n```\r\n\r\nThe value of client in asyncdispatch.nim:1197 is 0, then > 0 two times and -1 (invalid) after that.\r\n\r\nI'm on 64-bit linux"},"comment":{"url":"https://api.github.com/repos/nim-lang/Nim/issues/comments/158006025","html_url":"https://github.com/nim-lang/Nim/issues/3518#issuecomment-158006025","issue_url":"https://api.github.com/repos/nim-lang/Nim/issues/3518","id":158006025,"user":{"login":"tulayang","id":6581640,"avatar_url":"https://avatars.githubusercontent.com/u/6581640?v=3","gravatar_id":"","url":"https://api.github.com/users/tulayang","html_url":"https://github.com/tulayang","followers_url":"https://api.github.com/users/tulayang/followers","following_url":"https://api.github.com/users/tulayang/following{/other_user}","gists_url":"https://api.github.com/users/tulayang/gists{/gist_id}","starred_url":"https://api.github.com/users/tulayang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tulayang/subscriptions","organizations_url":"https://api.github.com/users/tulayang/orgs","repos_url":"https://api.github.com/users/tulayang/repos","events_url":"https://api.github.com/users/tulayang/events{/privacy}","received_events_url":"https://api.github.com/users/tulayang/received_events","type":"User","site_admin":false},"created_at":"2015-11-19T09:46:45Z","updated_at":"2015-11-19T09:46:45Z","body":"```\r\nserver.accept() -------> cb:  [cb1]\r\nserver.accept() -------> cb:  [cb1, cb2]\r\n\r\nWhen epoll trigger:\r\n    cb1() -> accept() OK\r\n    cb2() -> accept() -1      # because Non-blocking, it will return -1 \r\n```\r\n\r\nThis will be same result:\r\n\r\n```nim\r\n    clients = @[]\r\n    var server = newAsyncSocket()\r\n    server.bindAddr(12345.Port)\r\n    server.listen()\r\n\r\n    asyncCheck server.accept()   # OK\r\n    asyncCheck server.accept()   # -1\r\n\r\n    runForever()\r\n```\r\n\r\nI think you'd better not use accept like that. "}}