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

ssl.SSLError during handshake can teardown worker process #111

Merged
merged 1 commit into from
Oct 7, 2019

Conversation

normal-cock
Copy link
Contributor

prevent socket which is listening been close when TcpConnectionUninitializedException is raised

@normal-cock
Copy link
Contributor Author

The abnormal case is that, when TcpConnectionUninitializedException is raised the worker process close the listening socket making the worker not work any more.

@abhinavsingh
Copy link
Owner

I am wondering why is TcpConnectionUninitializedException being thrown here, we are just passing fileno=conn.fileno() to ProtocolHandler class, so connection is pretty much initialized.

TcpConnectionUninitializedException can still be thrown, for example, if client connection is closed, _conn object has been deleted but code is still trying to access the connection property (either to send or recv data). In which case, I think it's a programming bug.

Do you have an example stack trace for further investigation? Are you using proxy.py via a custom plugin? Thank you.

@abhinavsingh abhinavsingh changed the title bugfix TcpConnectionUninitializedException teardown worker process Oct 7, 2019
@normal-cock
Copy link
Contributor Author

In my case, TcpConnectionUninitializedException have been thrown as result of the failure of ssl handshake of when https is enabled. In this case, result of self.optionally_wrap_socket is None, so TcpConnectionUninitializedException is thrown.

class ProtocolHandler(threading.Thread):
   def __init__
        ...
        conn = self.optionally_wrap_socket(self.fromfd(fileno))
        if conn is None:
            raise TcpConnectionUninitializedException()

I'm not using proxy.py via a custom plugin. I use it with a self-signed certificate.

@abhinavsingh
Copy link
Owner

Makes sense, indeed ssl wrap can fail. However, I think we can improve here a little so that we can log more helpful context. Logging logger.error('TcpConnectionUninitializedException while handling new connection') isn't very clear of what actually happened. I probably just raised TcpConnectionUninitializedException without thinking twice about it.

How about we remove the except Exception from here https://github.com/abhinavsingh/proxy.py/blob/develop/proxy.py#L1543-L1561 so that SSL handshake exception is actually propagated back to worker.

Then in the worker we can:

  1. catch the exception
  2. log helpful message
  3. also shutdown/close client connection

Wdyt?

@normal-cock
Copy link
Contributor Author

Sounds better and I've changed the code.

proxy.py Outdated Show resolved Hide resolved
proxy.py Show resolved Hide resolved
prevent socket which is listening been close when TcpConnectionUninitializedException is raised
Copy link
Owner

@abhinavsingh abhinavsingh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks.

@abhinavsingh abhinavsingh changed the title TcpConnectionUninitializedException teardown worker process ssl.SSLError during handshake can teardown worker process Oct 7, 2019
@abhinavsingh abhinavsingh merged commit 542cd42 into abhinavsingh:develop Oct 7, 2019
abhinavsingh added a commit that referenced this pull request Oct 7, 2019
abhinavsingh added a commit that referenced this pull request Oct 10, 2019
* Initialize skeleton electron app

* Attempt to open devtools

* Electron free

* Initialize public/devtools

* Add basic support for static file serving and chrome devtools.

1. No cache header management for static file serving yet.
2. No chunked encoded responses for static files yet.
3. Chrome Devtool initialization.

* Fix static serving with query params

* profile using py-spy

* Complete websocket client loop

* lint check

* Add support for building websocket frames

* Remove redundant CDT params

* Lint check

* Refactor web server base plugin name

* Devtools integrated, need more polish

* Add START_TIME global var

* lint fix

* Remove outdated chrome rdp

* Add FAQs

* Add FAQs

* socket_connection decorator + context manager

* Defer SSL handshake and plugin initialize until protocol handler thread
has started.

This is a follow up to this PR
#111

* Add tests for new_socket_connection and its friend socket_connection

* Address an issue which came back after being fixed in #92

* Lint fixes

* uff ye str and bytes

* Remove explicit flushes outside of write ready descriptor handlers

* add links to import proxy

* Only try websocket upgrade if a route is registered

* Add plugin_examples.WebServerPlugin and use precision logging for levelname

* Remove redundant comments

* Add --devtools-ws-path flag

* Add on_websocket_open and on_websocket_close callbacks

* Add empty stubs for incomplete CDT responses

* Ensure client is ready before final flush

* Shutdown on write side of socket, may be client is still reading

* Since client.closed can be set, explicitly call client.connection.closed

* Add ModifyPostDataPlugin example.

Was first asked and referenced here
#115

* Start adding TestHttpProxyPlugin

* Fixes #116
@abhinavsingh abhinavsingh mentioned this pull request Oct 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Fix A PR sent for a bug fix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants