Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

socketserver can't stop #57958

Closed
teamnoir mannequin opened this issue Jan 9, 2012 · 10 comments
Closed

socketserver can't stop #57958

teamnoir mannequin opened this issue Jan 9, 2012 · 10 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@teamnoir
Copy link
Mannequin

teamnoir mannequin commented Jan 9, 2012

BPO 13749
Nosy @giampaolo, @bitdancer, @vadmium
Files
  • tryfixsocketserver.py
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2012-01-09.19:19:28.977>
    labels = ['type-bug', 'library']
    title = "socketserver can't stop"
    updated_at = <Date 2016-02-11.03:32:20.117>
    user = 'https://bugs.python.org/teamnoir'

    bugs.python.org fields:

    activity = <Date 2016-02-11.03:32:20.117>
    actor = 'martin.panter'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2012-01-09.19:19:28.977>
    creator = 'teamnoir'
    dependencies = []
    files = ['25148']
    hgrepos = []
    issue_num = 13749
    keywords = []
    message_count = 9.0
    messages = ['150965', '150966', '150968', '157103', '157109', '157674', '157677', '157712', '260071']
    nosy_count = 5.0
    nosy_names = ['giampaolo.rodola', 'r.david.murray', 'teamnoir', 'martin.panter', 'weirdink13']
    pr_nums = []
    priority = 'normal'
    resolution = None
    stage = 'test needed'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue13749'
    versions = ['Python 2.7', 'Python 3.2', 'Python 3.3']

    @teamnoir
    Copy link
    Mannequin Author

    teamnoir mannequin commented Jan 9, 2012

    Once I've instantiated my server class, along with a handler class, called server.serve_forever(), handler.handle() has been called, I've done my work, and I'm ready to shut the whole thing down...

    How do I do that?

    The doc says server.shutdown(), but if I call self.server.shutdown() from within handler.handle(), I seem to get a deadlock, which is exactly what I'd expect in a single threaded system with no way to "signal" the server.server_forever() loop which is several frames up the stack.

    I've also tried sys.exit() but it seems that the server object is catching that as an exception.

    How is this expected to work? How do I terminate the server.serve_forever() loop?

    Both 3.2 and 2.7 appear to behave the same way. The documentation is confusing here as it doesn't explain what is expected to happen in this case.

    @teamnoir teamnoir mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Jan 9, 2012
    @teamnoir
    Copy link
    Mannequin Author

    teamnoir mannequin commented Jan 9, 2012

    It appears as though the problem is that shutdown() blocks waiting for the serve_forever loop to terminate, which won't happen as long as the process is blocked on shutdown.

    I'd like to propose that the library be changed to eliminate the block. Shutdown() can set the flag and then return. This should allow the handler to return and the serve_forever loop to notice that it has been asked to cease operations.

    Failing that, I think the library needs some other way to exit a socketserver.

    @teamnoir
    Copy link
    Mannequin Author

    teamnoir mannequin commented Jan 9, 2012

    On second thought, my proposal is likely to break existing code, so I withdraw it.

    I don't know how to exit the server in a way that both works in all conditions and also continues to support existing semantics.

    I expect we'll need to create a new call. Perhaps "request_shutdown" which simply sets the flag without waiting?

    @weirdink13
    Copy link
    Mannequin

    weirdink13 mannequin commented Mar 29, 2012

    What about os._exit?
    or CTR-ALT-DEL (windows only)
    I'd say that socketserver.serveforever is very well named (Hey, if you can't turn it off then it's serving forever)

    @bitdancer
    Copy link
    Member

    At the very least the docs should be clarified to say that you have to call shutdown from a separate thread. (The one example of using it does do that.)

    I don't have a strong opinion about the proposed new method, not having used socketserver except for running in to it in stdlib test files. It sounds reasonable enough.

    @weirdink13
    Copy link
    Mannequin

    weirdink13 mannequin commented Apr 6, 2012

    what about this?
    def __init__(...):
    ...
    self.stop = False
    while True:
    (do stuff)
    if self.stop: break
    def quit(or whatever it's called): self.stop = True
    That would work without the backwards copatability issue right?

    @weirdink13
    Copy link
    Mannequin

    weirdink13 mannequin commented Apr 6, 2012

    Or even better:
    def __init__(...):
    ...
    self.stop = False
    while not self.stop:
    (do stuff)
    def quit(or whatever it's called): self.stop = True

    @weirdink13
    Copy link
    Mannequin

    weirdink13 mannequin commented Apr 7, 2012

    I tryed to fix the problem, here is my attemt.

    @vadmium
    Copy link
    Member

    vadmium commented Feb 11, 2016

    For stopping a single-threaded server from a request handler, perhaps see my patch for bpo-23430, which should allow calling sys.exit() or raising exceptions like SystemExit that do not inherit Exception.

    It seems to me that shutdown() can only sensibly be used from a separate thread. If a forking server is used, that thread would have to be in the initial server listening process. See also bpo-12463 for some proposals for changing shutdown().

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @iritkatriel
    Copy link
    Member

    This was documented in #83978.

    @iritkatriel iritkatriel closed this as not planned Won't fix, can't repro, duplicate, stale Mar 1, 2023
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants