matrix-rust-sdk icon indicating copy to clipboard operation
matrix-rust-sdk copied to clipboard

SyncService offline mode polls /versions every 100 ms

Open LEX0RE opened this issue 1 month ago • 7 comments

Hi,

I know there is a recnetly issue about something similar, but this is more about the loop of the poll for /version then something else. (Here is the issue I was thinking that would make the fix, but it seems to deviate a bit from it : https://github.com/matrix-org/matrix-rust-sdk/issues/7306 )

The problem right now is while I try to update my system (backend), it start spamming that request so much time that my firewall just block that IP cause it's really similar to a flood.

Could we make it exponential instead of only loop of 100 ms ?

For reference, I'm using Tuwunel with Sable client and the problem is updating Tuwunel just start all client to spam my server while it's just re-deploying

LEX0RE avatar Nov 05 '26 15:11 LEX0RE

Could we make it exponential instead of only loop of 100 ms ?

The intention for this is to be quite responsive, so that you quickly notice that the server is back.

That being said, we could tweak this a bit to make it more tolerable to deployments that are similar to yours.

The request itself will be retried 5 times using exponential backoff, in case the returned error is not considered to be permanent.

Any idea what kind of behavior or which retry values your firewall would tolerate?

poljar avatar Nov 06 '26 15:11 poljar

@poljar I'm using Crowdsec for the server firewall. I already remove the 404 request for the count for the flood as there was a lot that was returned. However, the /versions can't really be a 404 as that would also be confuse with real flood, so it's returning the Traefik noop@internal which is 418. The problem is more the number of request instead of the error itself.

At the end, yeah, the exponential backoff would be the best. I know that the goal is to be the most quick to notice that the server is back, so I if an option to activate the exponential exponential backoff is better, that would also work for me

LEX0RE avatar Nov 06 '26 17:11 LEX0RE

So here's the logic where we decide if we should use exponential backoff to retry a request or if exponential backoff doesn't make sense:

https://github.com/matrix-org/matrix-rust-sdk/blob/ed136c696b2988dd34e1ae4d60192193483d8e2d/crates/matrix-sdk/src/error.rs#L261-L287

If we return RetryKind::Permanent then the exponential backoff doesn't kick in and we fall back to the 100ms retry loop.

As you can see StatusCode::IM_A_TEAPOT is in the catch-all which returns RetryKind::Permanent. If you instead return a StatusCode::TOO_MANY_REQUESTS exponential backoff will kick in.

Though, if I understand your setup correctly, that's after your firewall bans the client. It would probably be better to prevent the firewall from interfering. Do we have any idea what the error is before your firewall kicks in?

poljar avatar Nov 10 '26 21:11 poljar

Hey, thanks for the comeback.

I will try to adjust my setup to reduce the downtime of the redeploy, but mostly the problem is that while I redeploy the backend (inside a docker container), the router from traefik isn't present and goes to a fallback service that catch any request not services take and return 418. That's not where the ban is, it's more the fallback when nothing is resulting (I could probably change it to something else then 418, but giving what you said, it would make sense as it should serve as "there is no service right now for that path" and not that the service is having an error).

For the ban part, it's more that the number of request sent when there is an error (100ms in retry loop equal 10 req/s per user), is filling up a flood scenario in my Crowdsec to prevent bot to spam my service and banning them. However, these are not bot and are real user just connected on the client waiting for the server. When there is a ban, the HTTP Code returned is 403 (Forbidden). However, the ban is keep for at least 1 hour, and as the status_code you said, it still retry them at 10req/s for the duration of the ban (it's not big right now as I have like 3 users, but for people with more user, that would flood their setup mostly). To make it easy to understand, it's not the redeploy that make the ban, but the /version API request that get 4XX error that make him ban that IP.

At the end, I will try as I said to reduce mostly the downtime of the redeploy, but yeah, the problem is more that even if someone just remove entirely his backend server, all clients that was connected to him will flood him in no time. I will also try others things with that in mind to better make the exponential backoff kick in.

Thanks for your help :)

LEX0RE avatar Nov 11 '26 04:11 LEX0RE

I will try to adjust my setup to reduce the downtime of the redeploy, but mostly the problem is that while I redeploy the backend (inside a docker container), the router from traefik isn't present and goes to a fallback service that catch any request not services take and return 418.

Ok, could you set things up so a 503 (service unavailable) is returned instead? This will make the client go through the exponential backoff path. It also sounds like the more correct response code.

As an additional step we might increase the 100ms timeout to some 500ms or even 1s. Would that be enough?

I think that this would still leave the app pretty responsive.

poljar avatar Nov 12 '26 11:11 poljar

Ok, could you set things up so a 503 (service unavailable) is returned instead? This will make the client go through the exponential backoff path. It also sounds like the more correct response code.

Yeah, I will try to make it use 503 when the service is down. The only downside of this that I don't know of the impact is if "flood bot" or stuff like that will use that 503 as try more on that path or stop sending request to it. I will make some test for some days, thanks for the help :)

As an additional step we might increase the 100ms timeout to some 500ms or even 1s. Would that be enough?

I think that this would still leave the app pretty responsive.

Yeah, for sure that would help a lot. It would decrease the number of request by a factor of 5 / 10 just with that and that's a lot more manageable with a rate-limit bucket on Crowdsec by having a big capacity on it, the redeploy would have time to correctly be up before the capacity is filled.

Thanks a lot again for your help on this

LEX0RE avatar Nov 12 '26 15:11 LEX0RE

Alright, opened #7484 to make the permanent error case behave a bit nicer.

poljar avatar Nov 13 '26 09:11 poljar