From 5bea086140daee9e1d1160de624cedf57c91d075 Mon Sep 17 00:00:00 2001 From: Philipp Hoenisch Date: Wed, 13 Dec 2023 12:24:37 +0100 Subject: [PATCH] wip: reproduce timing issue if a position is getting resized we close the current contract and reopen a new one. It might happen that the task will pick it up in the meantime and set the to closed. This breaks the renew protocl because we expect the position to be in a certain state --- coordinator/src/bin/coordinator.rs | 2 +- coordinator/src/node/closed_positions.rs | 9 +++++++-- coordinator/src/node/resize.rs | 12 +++++++++++- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/coordinator/src/bin/coordinator.rs b/coordinator/src/bin/coordinator.rs index 075ec8ab7..15e6847f4 100644 --- a/coordinator/src/bin/coordinator.rs +++ b/coordinator/src/bin/coordinator.rs @@ -49,7 +49,7 @@ use tracing::metadata::LevelFilter; const PROCESS_PROMETHEUS_METRICS: Duration = Duration::from_secs(10); const PROCESS_INCOMING_DLC_MESSAGES_INTERVAL: Duration = Duration::from_millis(200); const EXPIRED_POSITION_SYNC_INTERVAL: Duration = Duration::from_secs(5 * 60); -const CLOSED_POSITION_SYNC_INTERVAL: Duration = Duration::from_secs(30); +const CLOSED_POSITION_SYNC_INTERVAL: Duration = Duration::from_secs(1); const UNREALIZED_PNL_SYNC_INTERVAL: Duration = Duration::from_secs(10 * 60); const CONNECTION_CHECK_INTERVAL: Duration = Duration::from_secs(30); diff --git a/coordinator/src/node/closed_positions.rs b/coordinator/src/node/closed_positions.rs index 1add8148e..f1dcc1882 100644 --- a/coordinator/src/node/closed_positions.rs +++ b/coordinator/src/node/closed_positions.rs @@ -11,6 +11,7 @@ pub fn sync(node: Node) -> Result<()> { .context("Failed to load open and closing positions")?; for position in open_and_closing_positions { + tracing::debug!(position = position.id, "Checking position"); let temporary_contract_id = match position.temporary_contract_id { None => { tracing::trace!(position_id=%position.id, "Position does not have temporary contract id, skipping"); @@ -20,9 +21,13 @@ pub fn sync(node: Node) -> Result<()> { }; let contract = match node.inner.get_closed_contract(temporary_contract_id) { - Ok(Some(closed_contract)) => closed_contract, + Ok(Some(closed_contract)) => { + tracing::debug!(position = position.id, "Position closed"); + + closed_contract + } Ok(None) => { - tracing::trace!(position_id=%position.id, "Position not closed yet, skipping"); + tracing::debug!(position_id=%position.id, "Position not closed yet, skipping"); continue; } Err(e) => { diff --git a/coordinator/src/node/resize.rs b/coordinator/src/node/resize.rs index d93d25e5b..48ca39a8a 100644 --- a/coordinator/src/node/resize.rs +++ b/coordinator/src/node/resize.rs @@ -98,7 +98,17 @@ impl Node { }, )?; - db::positions::Position::set_open_position_to_resizing(conn, position.trader.to_string())?; + let pool = self.pool.clone(); + tokio::spawn({ + let string = position.trader.to_string(); + let pool = pool.clone(); + async move { + let mut conn = pool.get()?; + tokio::time::sleep(core::time::Duration::from_secs(10)).await; + db::positions::Position::set_open_position_to_resizing(&mut conn, string)?; + anyhow::Ok(()) + } + }); Ok(()) }