Skip to content

Make middleware thread safe#150

Merged
DonSchado merged 1 commit intorailslove:masterfrom
datacamp-engineering:ik/make-it-thread-safe-ivars
Mar 23, 2020
Merged

Make middleware thread safe#150
DonSchado merged 1 commit intorailslove:masterfrom
datacamp-engineering:ik/make-it-thread-safe-ivars

Conversation

@kspe
Copy link
Copy Markdown
Contributor

@kspe kspe commented Feb 24, 2020

We've noticed that in high traffic threaded environment (Puma) this middleware shares instance variables memory between threads and occasionally swaps ivars content.

In a worst-case scenario observed for a request from user A the response that was coming back was from a request made by a user B. If Set-Cookie header was present user A was immediately logged out and logged in as a user B (completely confusing users).

We've observed also a number of other requests/responses swaps which in general were hard to debug e.g. API request calls returning non-API responses.

This PR does not remove using of ivars (@status, @headers, @body) but rather duplicates the middleware calls. We are using forked repo for a few days now and we do not observe the issue anymore.

For more issues similar to this one you can check here , here and here in SO thread.

If this won't be planned for merging after all, maybe at least will save some time for others debugging similar symptoms.

@bumi
Copy link
Copy Markdown
Contributor

bumi commented Feb 26, 2020

oh, thanks a lot for the PR! why are those instance variables in the first place, 🤔
I'd even try to get rid of those instance variables.
@DonSchado could you take a look? maybe merge this PR, release a patch and then these instance vars should be refactored?

@DonSchado
Copy link
Copy Markdown
Collaborator

Thanks for raising this issue @kspe very appreciated!
If we inline the html? method, we could remove all instance vars.

But very interesting, I also don't know if this pattern was just copied over from the former project, but now I believe the issue makes sense in the way rack initializes middleware.

Is #dup even necessary, when we refactor/remove the internal state?

@kspe
Copy link
Copy Markdown
Contributor Author

kspe commented Feb 28, 2020

Is #dup even necessary, when we refactor/remove the internal state?

I think the main issue is that middleware maintains a mutable state in ivars. Without those, it should be safe to remove dup. The worst thing is that it is hard to test in specs, easier with real applications run locally and bombarded with many requests at a high rate.

@bumi
Copy link
Copy Markdown
Contributor

bumi commented Mar 23, 2020

Thank you! <3

DonSchado added a commit that referenced this pull request Mar 25, 2020
…ges (#151)

see: #150

Co-authored-by: Marco <ms@donschado.de>
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