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

How to close a server? #127

Closed
danieljfarrell opened this issue Mar 7, 2020 · 5 comments
Closed

How to close a server? #127

danieljfarrell opened this issue Mar 7, 2020 · 5 comments

Comments

@danieljfarrell
Copy link
Contributor

danieljfarrell commented Mar 7, 2020

I want to start and stop a thriftpy2 service from inside a running desktop application. However, calling close on the server does not terminate it’s thread and the application hangs.

For example,

def start_button_clicked(self):
    self.server = make_server(...)
    self.thd = threading.Thread(target=server.serve)

def stop_button_clicked(self):
    self.server.close()
    self.thd.join() # <— hangs 
    self.server = None
    self.thd = None

Maybe I am doing something wrong?

I’m using Python 3.6.9 and thriftpy2 0.4.10 on Windows 10.

@danieljfarrell
Copy link
Contributor Author

danieljfarrell commented Mar 7, 2020

client, _ = self.sock.accept()

It seems the thread is blocking here, waiting for the socket to accept something.

@danieljfarrell
Copy link
Contributor Author

danieljfarrell commented Mar 7, 2020

I came across this similar issue on the deprecated project page, Thriftpy/thriftpy#295. If I kill the transport and the socket I can get the threat to terminate, however, it does so by raising an exception.

def stop_button_clicked(self):
    server.close()
    server.trans.close()  # <-- kill this
    server.trans.sock.close()  # <-- kill this, Exception is raised
    self.thd.join()  #  need to wrap in try-except to finish tear down 
    self.server = None
    self.thd = None

What would you like to do? Shall I prepare a pull request?

@ethe
Copy link
Member

ethe commented Mar 9, 2020

Hanging in accept means there is still at least one socket that is connection completed and waiting for transporting from the client, close it right now would make the client raise an exception.

@aisk
Copy link
Member

aisk commented Mar 9, 2020

Another way to solve this is you can set a timeout on server socket like this: server.trans.sock.settimeout(1). And the accept should in a dead loop to call this eventually, and with a flag to check should it closed.

@danieljfarrell
Copy link
Contributor Author

Thanks for the comments both! So there must be a client connection still open, otherwise the server should close?

Setting the timeout on the socket is a much better way to go.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants