Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8079801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:13:31+00:00 2026-06-05T16:13:31+00:00

While playing with standard library i’ve found a strange difference between python2 and python3.

  • 0

While playing with standard library i’ve found a strange difference between python2 and python3. If i try to catch a signal in python2 while TCPServer is running in a different thread the signal does not get handled, but in python3 it does.

Here is a script that reproduces the problem

import signal
import threading
import sys 
if sys.version_info > (3,0):
    from socketserver import TCPServer, BaseRequestHandler
else:
    from SocketServer import TCPServer, BaseRequestHandler

def shutdown(signum, frame):
    print("Shutting down server thread")
    server.shutdown()

server = TCPServer(
    ('127.0.0.1', 7654),
    BaseRequestHandler
)
signal.signal(signal.SIGTERM, shutdown)
signal.signal(signal.SIGINT, shutdown)
server_thread = threading.Thread(target=server.serve_forever)
print("Starting server thread")
server_thread.start()
print("Waiting for server thread to shut down")
server_thread.join()
print("Server thread terminated")

This is the output from python3:

Starting server thread
Waiting for server thread to shut down
^CShutting down server thread
Server thread terminated

And this is from python2:

Starting server thread
Waiting for server thread to shut down
^CKilled

“^C” is a keyboard interrupt and “Killed” is sigkill that i sent to a process.

Why shutdown was not called?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T16:13:33+00:00Added an answer on June 5, 2026 at 4:13 pm

    For me it seems thread.join() makes some lock and prevents from catching the signal.

    I’ve tested the following code in Python 2.7 and it seems to work:

    import time
    import signal
    import threading
    import sys 
    if sys.version_info > (3,0):
        from socketserver import TCPServer, BaseRequestHandler
    else:
        from SocketServer import TCPServer, BaseRequestHandler
    
    def shutdown(signum, frame):
        print("Shutting down server thread")
        server.running = False
        server.shutdown()
    
    server = TCPServer(
        ('127.0.0.1', 7654),
        BaseRequestHandler
    )
    signal.signal(signal.SIGTERM, shutdown)
    signal.signal(signal.SIGINT, shutdown)
    server_thread = threading.Thread(target=server.serve_forever)
    print("Starting server thread")
    server_thread.start()
    server.running = True
    print("Waiting for server thread to shut down")
    
    while server.running:
        time.sleep(1)
    
    server_thread.join()
    print("Server thread terminated")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After playing a while with Mono, I see one strange thing - every call
While playing with NLP I've been encountered with little problem: switch(var1) { case Variant1_1:
There is an eval() function in Python I stumbled upon while playing around. I
So, I am learning Javascript while playing white Google Calendar APIs and I just
A while back I was playing with methods using variable-length argument lists (java) that
I was playing with a fiddle earlier today while trying to answer a question
I was playing around with the following code: function recaller(){ while(x-- > 0)recaller(); }
So I've been playing with Akka Actors for a while now, and have written
I have been playing around with the Silverlight progressbar a while and no matter
<video> element is upcoming cross browser standart for playing videos, while most videos out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.