Repository navigation
Fix bug that makes AirflowSecurityManagerV2 leave transactions in the idle in transaction state - #39935
Conversation
…he `idle in transaction` state. The problem is due to the combined usage of the `@cached_property` and `@provide_session` decorators on top of the `_auth_manager_is_authorized_map` method. `@cached_property` makes the `create_session` context manager get consumed and not be available to the nested functions.
|
Congratulations on your first Pull Request and welcome to the Apache Airflow community! If you have any issues or are unsure about any anything please check our Contributors' Guide (https://github.com/apache/airflow/blob/main/contributing-docs/README.rst)
|
vincbeck
left a comment
There was a problem hiding this comment.
Thank you for providing so much details in the description! That helps a lot to understand the issue. (And thanks for fixing a bug I created :D)
|
Some tests are failing |
@vincbeck thanks for the heads up on the test failures. I'll take a look at those later today. Cheers. |
… just use the session object that we already have available through the Security Manager's `appbuilder` attribute.
|
let's add this fix to 2.9.2 |
added |
|
Is it possiblw to add test to avoid regression? |
…ssions and instead uses the session already available through the `appbuilder` object that is attached to it.
@eladkal great idea. In c2f6a15 I added a test that makes sure the Security Manager doesn't create extra DB sessions and instead uses the session already available through the The test fails for the previous version of the Security Manager. Cheers. |
|
Awesome work, congrats on your first merged pull request! You are invited to check our Issue Tracker for additional contributions. |
…he `idle in transaction` state (apache#39935)
…he `idle in transaction` state (apache#39935)
…he `idle in transaction` state (apache#39935)
Description
This PR fixes a bug that makes
AirflowSecurityManagerV2leave some transactions in theidle in transactionstate.The problem is due to the combined usage of the
@cached_propertyand@provide_sessiondecorators on top of the_auth_manager_is_authorized_mapmethod: the@cached_propertydecorator makes thecreate_sessioncontext manager get consumed and not be available to wrap the nested functions.The fix presented here just makes sure each of the resource-retrieving nested functions in
_auth_manager_is_authorized_mapcan use a session object with a clean state.Impact
This problem affects views that perform fine-grained access checks using resource ID (for example, editing variables, editing connections, etc).
Apart from the negative impact of having orphaned transactions in the
idle in transactionstate, this problem also may lead tosqlalchemy.exc.PendingRollbackErrorexceptions whilst trying to edit a resource.The sqlalchemy pool (using the default settings) seems to just chug along reusing the connections that have orphaned transactions, until they get invalidated. Then, if the
Sessionobject that is automatically passed to the security manager's nested functions gets to use one of those invalidated connections, an exception like the following is thrown:Tweaking the sqlalchemy pool settings and using pgbouncer also seem to play a role in mitigating this issue. In any case, there are already some users reporting errors editing resources. See #39581.
I'm not familiar with the connection invalidation mechanisms within sqlalchemy pools, but that's definitely a component in generating errors like the above.
I stumbled upon this problem in a Postgres-based environment after upgrading to Airflow 2.9.1 (Python 3.11). I don't know if other DB backends are equally affected.
Reproducing
idle in transactiontransactions:idle in transactionmonitor command show orphaned transactions that won't go away...until the webserver is restartedThe problem goes away by applying the code in this PR.
^ Add meaningful description above
Read the Pull Request Guidelines for more information.
In case of fundamental code changes, an Airflow Improvement Proposal (AIP) is needed.
In case of a new dependency, check compliance with the ASF 3rd Party License Policy.
In case of backwards incompatible changes please leave a note in a newsfragment file, named
{pr_number}.significant.rstor{issue_number}.significant.rst, in newsfragments.