
Post mortem: follow middleware or how we broke comments
Hello! We have not very good news: we made a mistake in the mobile version, which could disturb you all the holidays.
The essence of the problem: a person sends a comment to a post, sees it with his username and leaves the page, but if you refresh the page, this comment will already be under a different username. This only worked if users were simultaneously on the page of one post.
According to our data, over the weekend, 774 comments were sent from the mobile version. Each of them could suffer.
We use a bunch of VueJS + NodeJS (Express, SSR).
NodeJS serves multiple connections in a single stream asynchronously, that is, it uses one instance for all clients. This means that global variables are initialized only once and live as long as the instance is alive.
Therefore, you must be extremely careful about the execution order of middlewar'ov, as well as the definition and redefinition of the values of variables (especially if they are global).
And this is what happened to us (this is a code example):
global.foo = 'bar';
app.get('/main', (req, res, next) => {
res.send(global.foo);
});
app.get('/change', (req, res, next) => {
global.foo = global.foo === 'bar' ? 'barbar': 'bar';
res.send(global.foo);
});
What will the server return?
- Client 1 on
/main
>>> 'bar' - Client 2 on
/change
>>> 'barbar' - Customer 1 again on
/main
>>> 'barbar'
The example, of course, is greatly simplified, but the principle is the same.
Now everything works as it should. Please forgive the inconvenience, and if you are affected by this problem, please contact us through the feedback form .
We are looking for an opportunity to return the comments to their rightful owners, we will tell you what came of it later.