@@ -1283,15 +1283,20 @@ void Engine::config_request(
12831283 const QJsonObject& json_obj,
12841284 std::function<void (const QJsonObject&)> callback)
12851285{
1286- int node_id = json_obj[" node_id" ].toInt ();
1286+ static std::atomic<int > request_counter{0 };
1287+ int request_id = ++request_counter;
12871288
1288- // Enqueue callback for this node_id
1289- config_callback_queue_[node_id].push_back (callback);
1289+ // Enqueue callback keyed by unique request_id, not node_id, so concurrent
1290+ // requests to the same node cannot steal each other's callbacks.
1291+ config_callback_queue_[request_id].push_back (callback);
1292+
1293+ QJsonObject req_obj = json_obj;
1294+ req_obj[" request_id" ] = request_id;
12901295
12911296 REST_requester* requester = new REST_requester (
12921297 std::bind (&Engine::config_response, this , std::placeholders::_1, std::placeholders::_2),
12931298 REST_requester::RequestType::REQUEST_CONFIG ,
1294- json_obj );
1299+ req_obj );
12951300
12961301 {
12971302 std::lock_guard<std::mutex> lock (requesters_mutex_);
@@ -1306,18 +1311,22 @@ void Engine::config_response(
13061311 if (!json_obj.empty ())
13071312 {
13081313 QJsonObject response_obj = json_obj[" response" ].toObject ();
1309- int node_id = response_obj[" node_id " ].toInt ();
1314+ int request_id = response_obj[" request_id " ].toInt ();
13101315
1311- auto it = config_callback_queue_.find (node_id );
1316+ auto it = config_callback_queue_.find (request_id );
13121317 if (it != config_callback_queue_.end () && !it->second .empty ())
13131318 {
13141319 auto cb = it->second .front ();
13151320 it->second .pop_front ();
13161321 cb (json_obj);
1322+ if (it->second .empty ())
1323+ {
1324+ config_callback_queue_.erase (it);
1325+ }
13171326 }
13181327 else
13191328 {
1320- std::cout << " WARNING config_response node_id =" << node_id
1329+ std::cout << " WARNING config_response request_id =" << request_id
13211330 << " but no queued callback" << std::endl;
13221331 }
13231332 }
0 commit comments