-
Notifications
You must be signed in to change notification settings - Fork 944
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
session
is prone to race conditions
#1372
Comments
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This should affect all quickly incoming messages handled in an async way. Sending a bunch of messages while the bot is not running is just a simple way to simulate that. At least that is what I currently understand about it. |
Can this ever be a problem for different users? If one user / chat is spamming like hell and change the session all the time I personally wouldn't care if the bot isnt doing exactly everything they spammed in the correct order without any strange effects. If this does result in other users having strange results while one chat is spamming like hell, this is a problem but I currently think that isnt the case here? |
With default I tested some other libraries:
|
Synchronous session middleware ( I believe asynchronous session middleware ( |
Is it only fixable in a major release because of public API changes to session? Would the use of mutex be overkill here? |
One worker already cannot run JS in paralell. Thus, mutex is probably overkill, and would probably require changing the interface. In synchronous sessions, stale reads are the problem: one task overwrites
Note, none of this prevents a task from overwriting a property within |
Personally I tend to move away from session for persistent data. If I have a bunch of lets say movies they are managed in their own way and ctx.session is not involved there while doing something with them. Session is too general for me most of the time anyway. I tend to use the session for short term "volatile" stuff like "current page in pagination" or object creation. This is either stuff with default values / optional checks anyway ( As the session is empty for new users or with telegraf/session after restarts the bot has to deal with empty sessions anyway. If the user manages to kill (some part of) the session content its not much different than a restart to me. Everything in my sessions is optional (or readonly) so existence has to be checked anyway. One example of one of my sessions in a not that complex bot. One of the main reasons why I like to use my inline menu and stateless question is their lack of stateful stuff required. Most of the state is embedded in the telegram updates (message text and callback data). They dont require much additional state / much in the session in the first place. Personally I think enforcing |
note that `telegraf` was not upgrated to v4 (major update) for now due to few breaking changes that this version introduces. More one this here: telegraf/telegraf#1372 (comment)
This comment has been minimized.
This comment has been minimized.
I'm not sure I understand this issue.
I could use this telegrafThrottler configuration to solve this problem ? telegrafThrottler({
in: {
maxConcurrent: 1,
},
}), |
Minimal Example Code Reproducing the Issue
Send a few messages, then start the bot. After it processes the messages, send one more.
Expected Behavior
Each
session
change is atomic:Current Behavior
ctx.session
is read before processing each update, and written only after processing has finished. Concurrent tasks might not notice each-others' modifications, last task to finish overwrites data:I believe every https://www.npmjs.com/search?q=telegraf-session- and https://github.com/nitreojs/puregram/tree/master/packages/session are affected as well.
cc @EdJoPaTo @KnightNiwrem @KnorpelSenf @mdsina @nitreojs @TemaSM
Solution?
Drop async
SessionStore
support (related: #1342), useObject.defineProperty
so that eachctx.session
access performs.get
,.set
, or.delete
.In the meantime, I'll advise
session
users to always use https://github.com/KnightNiwrem/telegraf-throttler, with default settings.The text was updated successfully, but these errors were encountered: