📜 ⬆️ ⬇️

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: the person sends a comment to the post, sees it with his login and leaves the page, but if you refresh the page, this comment will be under a different username. This only worked if users were on the same post page at the same time.


According to our data, over the weekend, 774 comments were sent from the mobile version. Each of them could suffer.


Few technical details

We use a bunch of VueJS + NodeJS (Express, SSR).


NodeJS serves multiple connections asynchronously at once, that is, it uses one instance per all clients. This means that global variables are initialized only once and live as long as the instance is alive.


Therefore, it is necessary to be extremely attentive to the order of execution of middlewar'ov, as well as to the definition and redefinition of the values ​​of variables (especially if they are global).


And that's what happened to us (this is sample code):


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'
  • Client 1 is again on /main >>> 'barbar'

An example, of course, is greatly simplified, but the principle is the same.


Now everything works as it should. Please excuse the inconvenience and, if you are affected by such a problem, contact us via the feedback form .


We are looking for an opportunity to return comments to their rightful owners, tell you what came of it later.


')

Source: https://habr.com/ru/post/443268/


All Articles