Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

sockets,threads and interupts

36 views
Skip to first unread message

loial

unread,
Sep 4, 2012, 11:26:21 AM9/4/12
to
I have threaded python script that uses sockets to monitor network ports.

I want to ensure that the socket is closed cleanly in all circumstances. This includes if the script is killed or interupted in some other way.

As I understand it signal only works in the main thread, so how can I trap interupts in my threaded class and always ensure I close the socket? Using KeyboardInterupt does not seem to work.


MRAB

unread,
Sep 4, 2012, 1:06:17 PM9/4/12
to pytho...@python.org
You could wrap it in try...finally. The 'finally' clause is guaranteed
to be run, so you can close the sockets there.

However, if the script is just killed, then it won't get the chance to
tidy up.

Grant Edwards

unread,
Sep 4, 2012, 2:11:12 PM9/4/12
to
That depends on the signal used to "kill" the thread.

You can catch SIGTERM and SIGINT and clean up before exiting.

You can't catch SIGKILL, but sending a SIGKILL isn't considered polite
unless you've already tried SIGTERM/SIGINT and it didn't work.

--
Grant Edwards grant.b.edwards Yow! Are we on STRIKE yet?
at
gmail.com

Ramchandra Apte

unread,
Sep 4, 2012, 11:43:11 PM9/4/12
to
We could just use a "with" statement. Neater, easier.

Dieter Maurer

unread,
Sep 5, 2012, 1:56:02 AM9/5/12
to pytho...@python.org
loial <jldun...@gmail.com> writes:

> I have threaded python script that uses sockets to monitor network ports.
>
> I want to ensure that the socket is closed cleanly in all circumstances. This includes if the script is killed or interupted in some other way.

The operating system should close all sockets automatically when
the process dies. Thus, if closing alone is sufficient...

Ramchandra Apte

unread,
Sep 5, 2012, 8:54:41 AM9/5/12
to pytho...@python.org
At least on Linux, if you kill a process using sockets, it takes about 10 seconds for socket to be closed. A program should try to close all resources. OS'es may take a long time to close a unclosed socket automatically.

Ramchandra Apte

unread,
Sep 5, 2012, 8:54:41 AM9/5/12
to comp.lan...@googlegroups.com, pytho...@python.org
On Wednesday, 5 September 2012 11:26:16 UTC+5:30, Dieter Maurer wrote:

Chris Angelico

unread,
Sep 5, 2012, 9:04:22 AM9/5/12
to pytho...@python.org
On Wed, Sep 5, 2012 at 10:54 PM, Ramchandra Apte <manian...@gmail.com> wrote:
> At least on Linux, if you kill a process using sockets, it takes about 10 seconds for socket to be closed. A program should try to close all resources. OS'es may take a long time to close a unclosed socket automatically.

Err, that's not my experience. When a process terminates, its
resources are released promptly.

ChrisA

Ramchandra Apte

unread,
Sep 5, 2012, 11:59:05 AM9/5/12
to pytho...@python.org
It is not guaranteed so a program shouldn't presume.
>
>
>
> ChrisA

Ramchandra Apte

unread,
Sep 5, 2012, 11:59:05 AM9/5/12
to comp.lan...@googlegroups.com, pytho...@python.org
On Wednesday, 5 September 2012 18:34:32 UTC+5:30, Chris Angelico wrote:

Ramchandra Apte

unread,
Sep 5, 2012, 12:01:18 PM9/5/12
to pytho...@python.org
oops forgot my signature
---
Bragging rights (I belong an exclusive community of banned people): got banned on SO and #python-offtopic
Projects:http://code.google.com/p/py2c/ and http://code.google.com/p/uniqos (name may be changed to PyOS)

Ramchandra Apte

unread,
Sep 5, 2012, 12:01:18 PM9/5/12
to comp.lan...@googlegroups.com, pytho...@python.org
On Wednesday, 5 September 2012 21:29:12 UTC+5:30, Ramchandra Apte wrote:

Bryan

unread,
Sep 5, 2012, 12:02:39 PM9/5/12
to
loial wrote:
> I have threaded python script that uses sockets to monitor network ports.
>
> I want to ensure that the socket is closed cleanly in all circumstances. This includes if the script is killed or interupted in some other way.
>
> As I understand it signal only works in the main thread, so how can I trap interupts in my threaded class and always ensure I close the socket?

You may have various threads waiting in blocking calls, and I don't
think there's a good way to alert them. Closing sockets that other
threads may be waiting on is "probably unwise" according to Linux man
page on close(2).

Do you really need to worry about it? If your process is being
forcibly terminated you probably cannot do anything better than the OS
will do by default.

-Bryan
Message has been deleted
0 new messages