codewars / codewars.com

Issue tracker for Codewars
https://www.codewars.com
BSD 2-Clause "Simplified" License
2.1k stars 220 forks source link

Comments are often double-posted when network is bad #3014

Open Voileexperiments opened 1 year ago

Voileexperiments commented 1 year ago

Recently I've been using CW on a 5G router. When the network becomes bad, I need to wait for a while before comments I post are accepted by the server and reflected on the page, but secretly these comments often become duplicated, and is unnoticeable until I refresh the page.

This can cause other problems, such as beta katas immediately sent to draft (because 2 issues are posted).

Normally this shouldn't happen. Is CW re-sending the request, or is the server somehow processing the same request twice?

hobovsky commented 1 year ago

Similar behavior has been already noticed and mentioned by B4B, me, and other users, but under different conditions: the reported behavior was that sometimes, when you try to write a post on a translation you just published, hitting the "POST" button appears to do nothing, so you click it again and again, only to find out that the posts counter actually goes up, multiple posts appear on the dashboard, and after a couple of minutes there's a bunch of posts under the translation. I cant remember it to be reported for other types of discourses than translations. I also don't recall it being reported as connected with some network issues, but in my case it didn't seem so (I was able to work with a translation, save it, publish it, and edit it without problems). Refresh did not show the posted comments, but they appeared after a minute or two.

I am not sure if these two are related or have a common root cause, but at least symptoms are very similar.

Voileexperiments commented 1 year ago

I only ever click the post button once though. When it gets stuck after a while I would refresh the page and try again.

Also, usually the duplicate comments are displayed as sent within the same minute, so it's probably not two different requests from before and after a refresh. (Though I'm not sure if refreshing the page when there's an ongoing request will trigger another same request to the server.)

Voileexperiments commented 1 year ago

This is happening again, I didn't inspect the network tab, but I caught the sight of two requests being sent 19 seconds apart:

image

This does seem like a client side framework issue.

Voileexperiments commented 1 year ago

So I finally found definite proof that this is a server side issue (I always knew, but without proof everyone always believe otherwise). According to a previous version of the HTTP/1.1 spec:

If an HTTP/1.1 client sends a request which includes a request body, but which does not include an Expect request-header field with the "100-continue" expectation, and if the client is not directly connected to an HTTP/1.1 origin server, and if the client sees the connection close before receiving any status from the server, the client SHOULD retry the request.

Basically it is expected that browsers (and HTTP clients in general) will automatically retry even non-idempotent requests if connection closes without any server response. This condition is actually very common in cellular or wifi networks where network switch from one base station to another, or if a LAN network is bad/crowded and keeps dropping connections/packets. So server side should handle this case and make sure the same request retried multiple times will not cause duplicate effects.