From c37ec2d9df84b82e5fbf98ca0f6bf13c90882100 Mon Sep 17 00:00:00 2001 From: Matthew Lam Date: Wed, 17 Apr 2024 11:22:36 -0700 Subject: [PATCH] Fix race condition in start_and_return (#1244) Fix race conditions when passing a BMv2 JSON Config into the command-line arguments when initializing SimpleSwitch. Fixes #1243 --- src/bm_sim/switch.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/bm_sim/switch.cpp b/src/bm_sim/switch.cpp index e360c5cea..5110a4d0d 100644 --- a/src/bm_sim/switch.cpp +++ b/src/bm_sim/switch.cpp @@ -75,14 +75,14 @@ SwitchWContexts::receive(port_t port_num, const char *buffer, int len) { void SwitchWContexts::start_and_return() { - { - std::unique_lock config_lock(config_mutex); - if (!config_loaded && !enable_swap) { - Logger::get()->error( - "The switch was started with no P4 and config swap is disabled"); - } - config_loaded_cv.wait(config_lock, [this]() { return config_loaded; }); + std::unique_lock config_lock(config_mutex); + if (!config_loaded && !enable_swap) { + Logger::get()->error( + "The switch was started with no P4 and config swap is disabled"); } + // We keep holding the lock as the start_and_return_ callback, which is + // implemented by targets, may access the P4 config for some validations. + config_loaded_cv.wait(config_lock, [this]() { return config_loaded; }); start(); // DevMgr::start start_and_return_(); // Starts any registered periodically-executing externs