[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Copas weirdness
- From: Fabio Mascarenhas <mascarenhas@...>
- Date: Tue, 5 Apr 2011 10:52:39 -0300
Hi, David,
copas.wrap does not put the socket in nonblocking mode for you. You
have to do it yourself, with :settimeout(0) in the raw socket before
calling the first :receive or :send in the wrapped socket. But notice
that copas reads and writes still block from the point of view of the
server thread, as the purpose of copas is to simulate multithreading.
If you really want fully async behavior in your socket handling (as in
node.js) you will have to use raw sockets with :settimeout(0) and do
the handling of timeouts yourself.
On the closing sockets, copas will close the socket in case an error
occurs (line 261 in copas.lua, function _doTick). The default behavior
is to log the error to stdout, though you can change it by setting
your own error handler by calling copas.setErrorHandler(func) in the
server thread, Copas will then call func with the error message, the
thread and the socket in case of errors. This might help diagnose the
problem.
--
Fabio Mascarenhas
On Mon, Apr 4, 2011 at 6:36 PM, David Given <dg@cowlark.com> wrote:
> I'm trying to write a proxy that tunnels TCP connections over a sequence
> of HTTP POST requests. (Yes, this is evil.) I'm using Lua and Copas for
> this as being the easiest way to do this kind of server. It's mostly
> working well except for... well, this.
>
> What I'm finding is that I can establish the tunnel, and pass data back
> and forth, and then eventually something will go wrong. This can
> manifest in various ways: either the proxy will block on a Copas read or
> write and stop responding to any other connections, or else one of the
> sockets will mysteriously close itself.
>
> I managed to persuade it to fail in the second mode often enough to do
> some debugging. Looking at the system call trace, I can see it do this:
>
> send(5, "\0\0\0\214\6
> \0\0\0\200\37U\22I\21k\"!\260\302\317\17\307\352&\220\33\327\2Eu\213"..., 144,
> 0) = 144
>
> ...as it sends some stuff to the upstream server; then I can see this:
>
> send(4, "HTTP/1.1 200 OK\r\nX-SessionId: 84"..., 40, 0) = 40
> fcntl64(4, F_GETFL) = 0x802 (flags O_RDWR|O_NONBLOCK)
> fcntl64(4, F_SETFL, O_RDWR) = 0
> close(4)
>
> ...as it tells the downstream server it's done it. This happens over and
> over again as data goes through. Then, eventually, this happens:
>
> send(5, "\0\0\0\f\n\25\0\0\0\0\0\0\0\0\0\0", 16, 0) = 16
>
> send(4, "HTTP/1.1 200 OK\r\nX-SessionId: 84"..., 40, 0) = 40
> fcntl64(5, F_GETFL) = 0x802 (flags O_RDWR|O_NONBLOCK)
> fcntl64(5, F_SETFL, O_RDWR) = 0
> close(5) = 0
>
> It seems to have just closed the wrong socket!
>
> My program, such as it is, is here: http://pastebin.com/yTXGh6xB (183
> lines, too antisocial to post). Am I going insane, or am I just making a
> stupid mistake somewhere?
>
> I'm on Debian, using stock Lua 5.1.4-5, copas 1.1.5-2, coxpcall
> 1.13.0-2, luasocket 2.0.2-4 respectively.
>
> --
> ┌─── dg@cowlark.com ───── http://www.cowlark.com ─────
> │
> │ "I have a mind like a steel trap. It's rusty and full of dead mice."
> │ --- Anonymous, on rasfc
>
>