Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

mgaller
Copy link

@mgaller mgaller commented Aug 20, 2020

MiddlewareMixin use sync_to_async with thread_sensitive to prevent errors in database connection clearance.
https://code.djangoproject.com/ticket/31905

Copy link
Member

@carltongibson carltongibson left a comment

Choose a reason for hiding this comment

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

Hi @mgaller — Thanks for this. Were you able to think about the regression test, perhaps in test_middleware_mixin.py?

@mgaller
Copy link
Author

mgaller commented Aug 20, 2020

@carltongibson
I tried that, see below

class MiddlewareMixinAsyncCorrectThreadTest(SimpleTestCase):
    async def get_response(self):
        return await sync_to_async(HttpResponse)('')

    async def get_middleware(self):
        class SimpleMiddleWare(MiddlewareMixin):
            def process_request(self, request):
                request.id_from_process_request = id(connections['default'])
                return request

            def process_response(self, request, response):
                return request.id_from_process_request, id(connections['default'])

        return await SimpleMiddleWare(self.get_response)(HttpRequest())

    def test_database_wrapper_ids(self):
        database_wrapper_ids = [*(async_to_sync(self.get_middleware)()), id(connections['default'])]
        self.assertEqual(len(database_wrapper_ids), 3)
        self.assertEqual(len(set(database_wrapper_ids)), 1)

It works, but I am not satisfied with it, maybe you are a little more successful.

@carltongibson
Copy link
Member

Hey @mgaller. Super thanks. I'll have a play. 👍

@carltongibson carltongibson force-pushed the master branch 2 times, most recently from dcc2921 to 77ca4b1 Compare August 25, 2020 10:29
Copy link
Member

@carltongibson carltongibson left a comment

Choose a reason for hiding this comment

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

Hi @mgaller. Thanks again for this.

I adjusted your test slightly, checking thread ID and connection — which AFAICS is about the best we can do... 🤔

I also added a release note, and rebased.

@felixxm Any thoughts? (Feel free to delete all the blank lines 😀)

@carltongibson carltongibson force-pushed the master branch 3 times, most recently from 4d102cc to e6bf7c3 Compare August 25, 2020 11:29
@felixxm felixxm changed the title Fixed #31905 -- MiddlewareMixin use sync_to_async with thread_sensiti… Fixed #31905 -- Made MiddlewareMixin call process_request()/process_response() with thread sensitive. Aug 26, 2020
@felixxm
Copy link
Member

felixxm commented Aug 26, 2020

Thanks both 👍

@mgaller Welcome aboard ⛵

…esponse() with thread sensitive.

Co-authored-by: Carlton Gibson <[email protected]>
@felixxm felixxm merged commit 547a07f into django:master Aug 26, 2020
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

Successfully merging this pull request may close these issues.

3 participants