I have finally found the reason why I can't reliably reproduce the
bug. It is math.random() in copas' send. :-)

Here is how to reproduce:

1. Change send to always yield (or use copas.lua I've attached)

   if true or (math.random(100) > 90) then
       _writing_log[client] = os.time()
       coroutine.yield(client, _writing)
   end

2. Launch attached copas_s3.lua

$ lua -lluarocks.require copas_s3.lua

3. Launch two clients to 127.0.0.1:50000 (I use nc in separate shells)

$ cat | nc 127.0.0.1 50000 # shell 1

$ cat | nc 127.0.0.1 50000 # shell 2

4. On shell 1 issue a lock command:

$ cat | nc 127.0.0.1 50000 # shell 1
LOCK<CRLF>

5. On shell 2 issue an unlock command:

$ cat | nc 127.0.0.1 50000 # shell 2
ULCK<CRLF>

Shell 1 gets disconnected, shell 2 is still connected.

Unlock command simply returns from handler after data is sent to
locked connection (see copas_s3.lua:87) . It is second shell's
connection that should be closed. However, copas closes first shell
connection (because it came in as argument with send()'s yield).

This is a really nasty issue that bites us painfully (I also suspect
that some other unstable behaviour sympthoms we get are related to it
as well).

If I'm doing something wrong, please tell me what is the right way to
handle suspended connections in copas. If it is a bug in copas, please
advise how to fix or workaround it.

If I should provide any extra information, please say so as well.

Alexander.

On Mon, Dec 14, 2009 at 19:48, Alexander Gladysh <[email protected]> wrote:
> Hi, list!
>
> I'm on Ubuntu 9.10 x64, Lua 5.1.4. List of installed rocks below.
>
> We're debugging a concurrency bug in our code.
>
> I'm still trying to build a minimal reproducible example, but I've
> decided to post this early in case someone get an idea. Some psionic
> debugging skills are required to read further. :-)
>
> Info I give here is a bit vague, sorry. But I'm able to reliably
> reproduce the bug. So, if any specific details are required, I should
> be able to provide them.
>
> 1. I have a copas-based server with two commands:
>
> PUT <channel> <data>
>
> If there is client's connection subscribed to this channel (see
> below), sends data to that connection immediately and unsubscribes
> client.
>
> Otherwise puts data to channel's FIFO queue.
>
> FETCH <channel>
>
> If channel's queue is not empty, gets front message and sends it to client.
>
> Otherwise, subscribes client to the channel (storing its connection).
>
> 2. I've got a test case with three clients:
>
> * One PUTs data into a channel (putter), closing and reopening
> connection each time.
> * Two others FETCH messages (fetchers). Fetchers fetch data from
> channels in a loop. Fetchers use single connection without closing it.
>
> After a time, either one of fetchers fails with error that its
> connection was unexpectedly closed.
>
> Debugging shows that connection is closed on the following scenario:
>
> 1. Fetcher subscribes to a channel, its connection is stored away.
> 2. After some time, putter sends PUT for the channel and closes the 
> connection.
> 3. PUT handler detects that channel has subscibers, and sends data to
> fetcher via stored connection.
> 4. PUT handler detects that connection was closed and exits from coroutine.
> 5. Copas code detects that coroutine exited and closes the connection.
>
> The problem is that copas closes fetcher's connection instead of putters.
>
> I've added some logging to copas code (attached).
>
> Here is the log (somewhat boiled down):
>
> # Fetcher's connection 0x2248808 goes to wait
> XXX AFTER resume co: thread: 0x2243310 skt: tcp{client}: 0x2248808 XXX
> # Putter connects with data for the channel
> XXX ACCEPTED client: tcp{client}: 0x22d6698 input: tcp{server}:
> 0x2246758 handler: function: 0x2242a50
> XXX BEFORE resume co: thread: 0x21e44c0 skt: tcp{client}: 0x22d6698 XXX
> # PUT handler sends data to fetcher
> XXX AFTER resume co: thread: 0x21e44c0 skt: tcp{client}: 0x22d6698 XXX
> # Send returns
> XXX BEFORE resume co: thread: 0x21e44c0 skt: tcp{client}: 0x2248808 XXX
> # No more data in connection
> [2009-12-14 15:47:55.958395] {28237} [BCO!] failed to read command:
> closed tcp{client}: 0x22d6698
> # Coroutine closes
> XXX AFTER resume co: thread: 0x21e44c0 skt: tcp{client}: 0x2248808 XXX
> # Copas closes the wrong connection
> XXX closing tcp{client}: 0x2248808 CO thread: 0x21e44c0 OK: true RES:
> nil NEW_Q: nil XXX here
>
> Any ideas? May I provide any extra info?
>
> Alexander.
>
> P.S. Sorry, I can't remember if I've modified copas.lua after I've got
> the log. Anyway, the meaning is still the same.
>
> $ luarocks list
>
> Installed rocks:
> ----------------
>
> copas
>  1.1.5-1 (installed) - /usr/local/lib/luarocks/rocks
>
> coxpcall
>  1.13.0-1 (installed) - /usr/local/lib/luarocks/rocks
>
> luafilesystem
>  1.5.0-1 (installed) - /usr/local/lib/luarocks/rocks
>
> luaposix
>  5.1.2-1 (installed) - /usr/local/lib/luarocks/rocks
>
> luasocket
>  2.0.2-2 (installed) - /usr/local/lib/luarocks/rocks
>
> rings
>  1.2.3-1 (installed) - /usr/local/lib/luarocks/rocks
>
> wsapi
>  cvs-3 (installed) - /usr/local/lib/luarocks/rocks
>
> wsapi-fcgi
>  cvs-2 (installed) - /usr/local/lib/luarocks/rocks
>
> xavante
>  2.1.0-1 (installed) - /usr/local/lib/luarocks/rocks
>

Attachment: copas.lua
Description: Binary data

Attachment: copas_s3.lua
Description: Binary data

_______________________________________________
Kepler-Project mailing list
[email protected]
http://lists.luaforge.net/cgi-bin/mailman/listinfo/kepler-project
http://www.keplerproject.org/

Reply via email to