Detecting a socket dropping in multi-threaded app


(Firestone, Mark - Technical Support) #1

How about this? My @who data structure is shared with a mutex between the
threads. It keeps a list of the thread id with each person’s username and
whatever who is logged on. When someone hangs up, Ruby just kills the
thread (I verified this with an experiment last night…)

I write a thread that wakes up every so often and checks Thread.current, and
compares that list with the list in @who, and if someone is missing from
Thread.current, zaps them from the list.

Is this a reasonable idea?

Thanks,

Mark

From: Jonathan Gillette [SMTP:jonathan@inetz.com]
Sent: 21 June 2002 21:56
To: ruby-talk@ruby-lang.org
Subject: Re: detecting a socket dropping in multi-threaded app

Yeah, I forgot your client would be a simple telnet session. In the same
vein as a ping, you could have a thread watching for inactive threads and
shutting them down. So you could keep a timestamp on the last time a
thread read a byte and shutdown the thread if too much time elapsed. In
my Ruby socket servers, I tend to stay away from trying to catch all the
possible exceptions and just keep a single thread that monitors the
others. It’s just less of a headache. I guess I hate having to worry
about missing an exception.

Jonathan Gillette
iNetZ Media

NOTICE: This e-mail and any attachment(s) may contain confidential and
proprietary information of Goss International Corporation and/or its
subsidiaries and may be legally privileged. This e-mail is intended solely
for the addressee. If you are not the addressee, dissemination, copying or
other use of this e-mail or any of its content is strictly prohibited and
may be unlawful. If you are not the intended recipient please inform the
sender immediately and destroy the e-mail and any copies. All liability for
viruses is excluded to the fullest extent permitted by law. Any views
expressed in this message are those of the individual sender. No contract
may be construed by this e-mail.

···

-----Original Message-----


(Jonathan Gillette) #2

Yep. That’s a reasonable idea. As long as you’re sure the user’s Thread dies, this should work.

···

Firestone, Mark - Technical Support (mark.firestone@gossgraphic.co.uk) wrote:

How about this? My @who data structure is shared with a mutex between the
threads. It keeps a list of the thread id with each person’s username and
whatever who is logged on. When someone hangs up, Ruby just kills the
thread (I verified this with an experiment last night…)

I write a thread that wakes up every so often and checks Thread.current, and
compares that list with the list in @who, and if someone is missing from
Thread.current, zaps them from the list.

Is this a reasonable idea?

Thanks,

Mark

Jonathan Gillette
iNetZ Media


(Paul Brannan) #3

Why not wrap your thread with an ensure block that removes the person
from the list when it is executed?

t = Thread.new do
# add the person to the list
begin
loop do
# Your logic here; when the socket (or other IO object) goes
# away, break out of the loop.
end
ensure
# remove the person from the list
end
end

This reduces possibility for reading from a socket that isn’t there, or
for doing something for a user who isn’t logged in.

(I never received the original message, btw, so I’m not 100% sure I
understand exactly what the problem is).

Paul

···

Firestone, Mark - Technical Support (mark.firestone@gossgraphic.co.uk) wrote:

How about this? My @who data structure is shared with a mutex between the
threads. It keeps a list of the thread id with each person’s username and
whatever who is logged on. When someone hangs up, Ruby just kills the
thread (I verified this with an experiment last night…)

I write a thread that wakes up every so often and checks Thread.current, and
compares that list with the list in @who, and if someone is missing from
Thread.current, zaps them from the list.

Is this a reasonable idea?