Skip to content

Conversation

@amanda11
Copy link
Contributor

@amanda11 amanda11 commented Apr 19, 2021

When using SSL to connect to mongo using certificates, was getting recursion error in st2stream, and therefore commands like
st2 pack install
would hang.

Example of error:

2021-04-19 15:56:17,678 140415850279744 WARNING db_init [-] Retry on ConnectionError - Cannot connect to database default :
maximum recursion depth exceeded while calling a Python object

Applying same fix as made to st2api under #4834 to st2stream resolved the issue.

@pull-request-size pull-request-size bot added the size/XS PR that changes 0-9 lines. Quick fix/merge. label Apr 19, 2021
@amanda11 amanda11 added this to the 3.5.0 milestone Apr 19, 2021
@amanda11 amanda11 added the bug label Apr 19, 2021
import os
from st2common.util.monkey_patch import monkey_patch

monkey_patch()
Copy link
Member

Choose a reason for hiding this comment

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

I'm somewhat worried if that is the correct / right change - I know that in the past we intentionally only did monkey patching in the worker and not the parent process.

I guess technically if all the tests pass and we observe no weird behavior locally with CTRL+C and stopping gunicorn process, it's probably.

But having said that, I believe we do exactly the same thing for st2api and st2auth - and if we do, we should likely update all the affected places.

Copy link
Member

@Kami Kami Apr 19, 2021

Choose a reason for hiding this comment

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

Btw, which version of st2 + mongoengine + pymongo are you using?

That hanging looks exactly the same as the hanging PR which adds support for Python 3.8 + Mongo DB 4.4 uncovered which I believe I tracked to us not performing monkey patching early enough so pymongo obtains reference to non patched version of threading module which will cause all kinds of issues.

And from the symptoms you mentioned it indeed looks like it could be related to the same root cause (although in that case it's related to threading module and here it's likely related to ssl one).

Copy link
Contributor Author

@amanda11 amanda11 Apr 19, 2021

Choose a reason for hiding this comment

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

This was StackStorm 3.4.1 on EL8. Mongo 4.0 and python 3.6.
st2api, st2auth were connecting successfully but st2stream wasn't. (So applied same change that was made to st2api to st2stream).
st2stream seems to be the only one that monkey patches differently to the other gunicorn processes. Was there a reason for that?

So st2api and st2auth both monkey patch in wsgi.py and have removed the line to monkey patch in app.py. But st2stream was the odd one out.

Copy link
Contributor Author

@amanda11 amanda11 Apr 20, 2021

Choose a reason for hiding this comment

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

Problem might be fixed already in 3.5dev. Putting back to draft whilst investigate further.

Upgraded a 3.4.1 on CentOS 8 that was having problem to 3.5 dev, and couldn't reproduce - so although this change fixed 3.4.1, might not be required on 3.5dev.

Will look to investigate monkey patch changes in 3.5.dev...

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, st2stream is a bit special since it keeps a long running connections open and IIRC there were issues related to gracefully shutting down the server when performing monkey patching in the main gunicorn process as well.

Having said that - I do think your change is generally correct and right one (we do want to perform monkey patching as early as possible, before importing anything else), but if possible we should verify it doesn't negatively affect the server shutdown logic and / or only do that if we are running in the worker and not the main gunicorn process.

Copy link
Member

Choose a reason for hiding this comment

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

Also, with this logic we will call monkey_patch() twice in case stream service is started using regular API endpoint and not using gunicorn (st2stream/bin/st2stream), but this likely should not be an issue.

Copy link
Contributor Author

@amanda11 amanda11 Apr 20, 2021

Choose a reason for hiding this comment

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

@Kami It looks like this is fixed by 3.5dev-31 (4922329). This is the minimalist 3.5dev RPM that I can upgrade my 3.4.1 system to, to resolve the problem.
3.5dev-27 (0f8af40) from unstable repo has the problem. So fixed somewhere between those commits (and it's not just the upgrade of eventlet)

So we shouldn't need to patch 3.5. Am trying to track down what the change is that has fixed this and see if can find same fix for 3.4.1. So we don't have to move the monkey patch on 3.5 as the current master branch is ok - to resolve this problem.

So I'm going to suggest closing this PR and not merging... (Though I'd like to find out what fix could be applied to 3.4.1 to fix it...)

Copy link
Contributor Author

@amanda11 amanda11 Apr 20, 2021

Choose a reason for hiding this comment

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

3.5dev-27 + gunicorn==20.1.0 works.
Will try a 3.4.1 install with gunicorn upgraded to see if that fixes it.

Yeah - 3.4.1 + gunicorn==20.1.0 works.

I might translate this to an issue - so that others know the workaround...

Copy link
Member

Choose a reason for hiding this comment

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

Per my comment above, I still think it would be better if we can try to move monkey patching to as early as possible (before any other imports) - that's the right way to do it.

Doing it later on has no guarantee it will always work and won't break in the feature. Technically, even if some module obtain a reference to a patched module which is patched later on, patching logic will try to replace that module in sys.module, but there is no guarantee that this will always work correctly (depends on how reference to the module is obtained if some hacks are involved and also what's done with the module before it's monkey patched - technically if it's just imported and not used until post monkey patch it should work, but that's not always the case and sometimes imports have side affects).

@amanda11 amanda11 marked this pull request as draft April 20, 2021 08:23
@Kami
Copy link
Member

Kami commented Apr 21, 2021

@amanda11 I tested this change locally using gunicorn and non-gunicorn entry point with active connections and it seems to be working correctly, including the shut down phase.

So as per my comment above, I think it's probably still a good idea to merge this (to avoid surprises in the future).

@amanda11
Copy link
Contributor Author

Thanks so much for that, I was going to do some further testing after last comment - but was on leave. But I will merge them if all good, and it's great to have had someone test who knew about previous problems - as didn't want to break it!

@amanda11 amanda11 marked this pull request as ready for review April 21, 2021 20:52
@amanda11
Copy link
Contributor Author

@Kami - un-drafted - so just need an approval review and then I can merge this.
Thanks so much for your help and feedback.

Copy link
Member

@Kami Kami left a comment

Choose a reason for hiding this comment

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

LGTM and thank you 👍

@amanda11
Copy link
Contributor Author

Just realised I missed the CHANGELOG! - will add that and then merge...

@pull-request-size pull-request-size bot added size/S PR that changes 10-29 lines. Very easy to review. and removed size/XS PR that changes 0-9 lines. Quick fix/merge. labels Apr 21, 2021
@amanda11 amanda11 merged commit 5fa48d1 into master Apr 22, 2021
@amanda11 amanda11 deleted the monkey_patch_st2stream branch April 22, 2021 12:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug size/S PR that changes 10-29 lines. Very easy to review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants