clementfarabet / async

An async framework for Lua/Torch.
116 stars 35 forks source link

setting timeout triggers assertion error #24

Open eulerreich opened 9 years ago

eulerreich commented 9 years ago

I have the following code for the TCP server (modified from the example) in a file named tcp-server.lua.

async = require 'async'
async.tcp.listen({host='localhost', port=4321}, function(client)
   -- Receive:
   client.ondata(function(chunk)
      -- Data:
      print('received: ' .. chunk)

      -- Reply:
      client.write('1')
   end)

   -- Done:
   client.onend(function()
      print('client gone...')
   end)
end)
async.go()

I have the following for a function that retrieves the message sent by the server.

async = require 'async'
function get_msg()
    local msg = nil
    async.tcp.connect({host='localhost', port=4321}, function(client)
        -- Write something
        client.write('something .. ')

        -- Callbacks
        client.ondata(function(chunk)
            print('received: ' .. chunk)
            msg = chunk
            client.close()
        end)

        -- Done:
        client.onend(function()
            print('connection closed...')
        end)
        async.setTimeout(10, function()
            if not silent then
                print('timed out')
            end
            client.close()
        end)
    end)
    async.go()
    return msg
end

I ran th tcp-server.lua in one terminal and then calling get_msg() in a th shell (in another process). The result is

luajit: src/unix/stream.c:1074: uv_shutdown: Assertion `((stream)->io_watcher.fd) >= 0' failed.                                                 
Aborted (core dumped)  

If I comment out the part starting with async.setTimeout then it will return the value as intended. Also if I comment out client.close() in ondata then it will work as well.

The intention is to return the value as soon as possible, but if the server takes too long then return nil on timeout. I would think this is a fairly common scenario? Is this not the right way to do it?