-
Notifications
You must be signed in to change notification settings - Fork 101
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
feat(lp_bot): cancel order on stopping mm2 context #1143
Conversation
Which method of mitigation does this implement? |
The last one |
Confirm orders are cancelled if stopping mm2 gracefully. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the fix!
Could you please consider my comments?
.read() | ||
.await | ||
.dispatch_async(ctx.clone(), event.into()) | ||
.await; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about adding a static function dispatch_lp_event
that will look like:
pub fn dispatch_lp_event(ctx: MmArc, event: LpEvents) -> Result<(), MmError<DispatcherError>> {
let dispatcher_ctx = DispatcherContext::from_ctx(&ctx).or_mm_error(|| Error::MmCtxNotAvailable)?;
dispatcher_ctx.dispatcher.read().await.dispatch_async(ctx, event).await;
Ok()
}
I think we can add it because we're supposed to produce events from a lot different places in the future, and it will reduce amount of code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds like a good idea, will do!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please replace dispatcher_ctx.dispatcher.read().await.dispatch_async(ctx.clone(), event.into()).await;
with dispatch_lp_event().await
?
mm2src/rpc/lp_commands.rs
Outdated
.read() | ||
.await | ||
.dispatch_async(ctx.clone(), event.into()) | ||
.await; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the function MmCtx::on_stop
is called before the member of the structure are destroyed? In this case that's probably a good idea!
Also I'd say that I think using What if an event listener adds another listener when dispatches an event?
But it's more likely to face the deadlock if we use I think it's out of this PR because I'm afraid that there are other places where we may face the deadlock too. |
It's exactly the reason that I decided to use
Here I think there is a possible long lock (not a deadlock because the lock will be released). Allowing multiple readers as the same time may be a solution for this kind of use case too. My advice to avoid deadlock when using the dispatcher: Add the events that you would like to listen too at the creation of the object / context. Remove them when you shutdown gracefully your app or your context. |
- A later refactor of stop/on_stop behaviour of the context will happen in another pull request
Summary of the last fixes:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only one thing
mm2src/mm2_lib/mm2_native_lib.rs
Outdated
@@ -233,6 +235,7 @@ pub extern "C" fn mm2_stop() -> i8 { | |||
}, | |||
}; | |||
|
|||
block_on(dispatch_lp_event(ctx.clone(), StopCtxEvent {}.into())); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just one question: are we planning to add fields to the StopCtxEvent
structure someday? If not, I'd suggest making it as pub struct StopCtxEvent;
to simplify it an instance creation: StopCtxEvent.into()
.
.read() | ||
.await | ||
.dispatch_async(ctx.clone(), event.into()) | ||
.await; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please replace dispatcher_ctx.dispatcher.read().await.dispatch_async(ctx.clone(), event.into()).await;
with dispatch_lp_event().await
?
…simple_market_maker.rs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the changes! Approve!
We decided not to use |
No description provided.