sockets,threads and interupts

L

loial

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.
 
M

MRAB

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.
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.
 
G

Grant Edwards

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.

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.
 
R

Ramchandra Apte

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

We could just use a "with" statement. Neater, easier.
 
D

Dieter Maurer

loial said:
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...
 
R

Ramchandra Apte

The operating system should close all sockets automatically when

the process dies. Thus, if closing alone is sufficient...

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.
 
R

Ramchandra Apte

The operating system should close all sockets automatically when

the process dies. Thus, if closing alone is sufficient...

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.
 
C

Chris Angelico

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
 
R

Ramchandra Apte

Err, that's not my experience. When a process terminates, its

resources are released promptly.

It is not guaranteed so a program shouldn't presume.
 
R

Ramchandra Apte

Err, that's not my experience. When a process terminates, its

resources are released promptly.

It is not guaranteed so a program shouldn't presume.
 
B

Bryan

loial said:
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
 
D

Dennis Lee Bieber

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.

Are you measuring the time for the socket to close, or the time
before the OS allows the socket (IP/Port) to be reused?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,692
Latest member
JenniferTi

Latest Threads

Top