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

#12049 Use valid "clock" in TLSMemoryBIOFactory #12049

Closed
wants to merge 1 commit into from

Conversation

ddzialak
Copy link

@ddzialak ddzialak commented Dec 2, 2023

Scope and purpose

Fixes #12048

Without this change, using IOCPReactor with TLS and Keep-Alive
will cause that response from server will be cached and not sent to the client.

Contributor Checklist:

This process applies to all pull requests - no matter how small.
Have a look at our developer documentation before submitting your Pull Request.

Below is a non-exhaustive list (as a reminder):

  • The title of the PR should describe the changes and starts with the associated issue number, like “#9782 Remove twisted.news. #1234 Brief description”.
  • A release notes news fragment file was create in src/twisted/newsfragments/ (see: Release notes fragments docs.)
  • The automated tests were updated.
  • Once all checks are green, request a review by leaving a comment that contains exactly the string please review.
    Our bot will trigger the review process, by applying the pending review label
    and requesting a review from the Twisted dev team.

@ddzialak ddzialak changed the title Use valid "clock" in TLSMemoryBIOFactory #12049 Use valid "clock" in TLSMemoryBIOFactory Dec 2, 2023
Without this change, using IOCPReactor with TLS and Keep-Alive
will cause that response from server will be cached and not sent
to the client.
@ddzialak
Copy link
Author

ddzialak commented Dec 2, 2023

please review

@chevah-robot chevah-robot requested a review from a team December 2, 2023 08:35
@adiroiban
Copy link
Member

Thanks for the PR.

I think that we have a bigger issue here.
If no clock is passed to the TLS factory, it should just use the default reactor.

from twisted.internet import reactor

Do you have multiple reactors installed on your application ?

If somewhere later from your code you call from twisted.internet import reactor do you get the IOCP reactor of the select reactor?

@@ -0,0 +1 @@
Fixed IOCPReactor when using with TLS and Keep-Alive option.
Copy link
Member

Choose a reason for hiding this comment

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

I think that the fix and the root cause is more generic. This not only about TLS and keep-alive.

I still trying to understand what is going in here... but it looks like in the trunk branch TLSMemoryBIOFactory is using the default reactor... and somehow, that reactor is not the same reactor that you are using for your HTTP application.

I am not sure.

I self contained exemple would help a lot to find the root cause of this issue.

Thanks

Copy link
Author

Choose a reason for hiding this comment

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

Probably it is a consequence of that default:
https://github.com/twisted/twisted/blob/trunk/src/twisted/internet/default.py#L49

In our code we import only one reactor:

if is_windows:
    from twisted.internet.iocpreactor.reactor import IOCPReactor as Reactor
elif is_cygwin:
    # Beware, select is currently limited to 512 sockets, see:
    # https://bugs.python.org/issue28708
    from twisted.internet.selectreactor import SelectReactor as Reactor
else:
    from twisted.internet.epollreactor import EPollReactor as Reactor  # type: ignore

BUT we have defined our class:

from twisted.web.server import NOT_DONE_YET, Site

class _SfSite(Site):

and adding raising exception in selectreactor give traceback:

  File "c:\tmp\darek-testing\repo\starfish\starfish\src\sfutils\service\twisted_app_server.py", line 230, in handle_requests
    site = _SfSite(
           ^^^^^^^^
  File "c:\tmp\darek-testing\repo\starfish\starfish\src\sfutils\service\twisted_app_server.py", line 153, in __init__
    super().__init__(resource, **kwargs)
  File "C:\tmp\darek-testing\venv\Lib\site-packages\twisted\web\server.py", line 814, in __init__
    super().__init__(*args, **kwargs)
  File "C:\tmp\darek-testing\venv\Lib\site-packages\twisted\web\http.py", line 3226, in __init__
    from twisted.internet import reactor
  File "C:\tmp\darek-testing\venv\Lib\site-packages\twisted\internet\reactor.py", line 38, in <module>
    from twisted.internet import default
  File "C:\tmp\darek-testing\venv\Lib\site-packages\twisted\internet\default.py", line 55, in <module>
    install = _getInstallFunction(platform)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\tmp\darek-testing\venv\Lib\site-packages\twisted\internet\default.py", line 49, in _getInstallFunction
    from twisted.internet.selectreactor import install
  File "C:\tmp\darek-testing\venv\Lib\site-packages\twisted\internet\selectreactor.py", line 199, in <module>
    raise AssertionError("OPS")

Copy link
Member

@adiroiban adiroiban Dec 3, 2023

Choose a reason for hiding this comment

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

it's ok to have a custom site.

Just to make sure
So, for the reactor, I guess that you have something like this at the very start of your app

from twisted.internet import iocpreactor
iocpreactor.install()

# Then everywhere else:
from twisted.internet import reactor

The documentation is here ... not sure if the docs are good enough

https://docs.twisted.org/en/stable/core/howto/choosing-reactor.html#core-howto-choosing-reactor-win32-iocp


We also need an automated test to make sure we will never reintroduce this bug, ever again :)

Copy link
Author

Choose a reason for hiding this comment

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

We don't have iocpreactor.install() because we have our custom Reactor that is a subclass of IOCPReactor and it wasn't clear that "install" is needed. Anyway, as I see these "install()" functions just create reactor instance and invoke main.installReactor(reactor).
I've verified that after invoking "main.installReactor(reactor)" problem also disappeared so I will go for that, closing that issue.

Copy link
Member

Choose a reason for hiding this comment

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

Happy to hear this was fixed.

Intall part is required, even for custome reactors.

This is what makes a singleton for the default reactor.

@adiroiban adiroiban requested a review from a team December 2, 2023 18:52
@ddzialak ddzialak closed this Dec 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

After upgrade from 22.10.0 to 23.10.0 HTTP server does not reply when using with SSL and keep-alive
3 participants