Skip to content
This repository was archived by the owner on Oct 23, 2024. It is now read-only.

Commit 8b4afdb

Browse files
committed
Fix race conditions by minimizing lock usages.
1 parent 849bd3b commit 8b4afdb

File tree

4 files changed

+11
-9
lines changed

4 files changed

+11
-9
lines changed

source/agent/addons/quic/QuicTransportConnection.cc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,14 @@ NAUV_WORK_CB(QuicTransportConnection::onStreamCallback)
9393
if (obj == nullptr || obj->m_streamsToBeNotified.empty()) {
9494
return;
9595
}
96-
std::lock_guard<std::mutex> lock(obj->m_streamQueueMutex);
9796
while (!obj->m_streamsToBeNotified.empty()) {
98-
v8::Local<v8::Object> streamObject = QuicTransportStream::newInstance(obj->m_streamsToBeNotified.front());
97+
obj->m_streamQueueMutex.lock();
98+
auto quicStream=obj->m_streamsToBeNotified.front();
99+
obj->m_streamsToBeNotified.pop();
100+
obj->m_streamQueueMutex.unlock();
101+
v8::Local<v8::Object> streamObject = QuicTransportStream::newInstance(quicStream);
99102
QuicTransportStream* stream = Nan::ObjectWrap::Unwrap<QuicTransportStream>(streamObject);
100-
obj->m_streamsToBeNotified.front()->SetVisitor(stream);
103+
quicStream->SetVisitor(stream);
101104
Nan::MaybeLocal<v8::Value> onEvent = Nan::Get(obj->handle(), Nan::New<v8::String>("onincomingstream").ToLocalChecked());
102105
if (!onEvent.IsEmpty()) {
103106
v8::Local<v8::Value> onEventLocal = onEvent.ToLocalChecked();
@@ -110,7 +113,6 @@ NAUV_WORK_CB(QuicTransportConnection::onStreamCallback)
110113
} else {
111114
ELOG_DEBUG("onEvent is empty");
112115
}
113-
obj->m_streamsToBeNotified.pop();
114116
}
115117
}
116118

source/agent/addons/quic/QuicTransportServer.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include <mutex>
1414
#include <nan.h>
1515
#include <string>
16-
#include <thread>
1716
#include <unordered_map>
1817

1918
class QuicTransportServer : public Nan::ObjectWrap, owt::quic::QuicTransportServerInterface::Visitor, QuicTransportConnection::Visitor {

source/agent/addons/quic/QuicTransportStream.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ NAN_METHOD(QuicTransportStream::write)
9797
QuicTransportStream* obj = Nan::ObjectWrap::Unwrap<QuicTransportStream>(info.Holder());
9898
uint8_t* buffer = (uint8_t*)node::Buffer::Data(info[0]->ToObject());
9999
unsigned int length = info[1]->Uint32Value();
100-
obj->m_stream->Write(buffer, length);
101-
info.GetReturnValue().Set(info.This());
100+
auto written = obj->m_stream->Write(buffer, length);
101+
info.GetReturnValue().Set(Nan::New(static_cast<int>(written)));
102102
}
103103

104104
NAN_METHOD(QuicTransportStream::addDestination)
@@ -131,7 +131,7 @@ v8::Local<v8::Object> QuicTransportStream::newInstance(owt::quic::QuicTransportS
131131

132132
void QuicTransportStream::MaybeReadContentSessionId()
133133
{
134-
if (!m_receivedContentSessionId) {
134+
if (!m_receivedContentSessionId && m_stream->ReadableBytes() > 0) {
135135
// Match to a content session.
136136
if (m_stream->ReadableBytes() > 0 && m_stream->ReadableBytes() < uuidSizeInBytes) {
137137
ELOG_ERROR("No enough data to get content session ID.");

source/agent/quic/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ module.exports = function (rpcClient, selfRpcId, parentRpcId, clusterWorkerIP) {
3939
new Map(); // Key is publication ID, value is stream pipeline.
4040
const outgoingStreamPipelines =
4141
new Map(); // Key is subscription ID, value is stream pipeline.
42+
let quicTransportServer;
4243

4344
const notifyStatus = (controller, sessionId, direction, status) => {
4445
rpcClient.remoteCast(controller, 'onSessionProgress', [sessionId, direction, status]);
@@ -53,7 +54,7 @@ module.exports = function (rpcClient, selfRpcId, parentRpcId, clusterWorkerIP) {
5354
return;
5455
}
5556
log.info('path is '+path.resolve(global.config.quic.keystorePath));
56-
const quicTransportServer = new QuicTransportServer(
57+
quicTransportServer = new QuicTransportServer(
5758
addon, global.config.quic.port, path.resolve(global.config.quic.keystorePath),
5859
password);
5960
quicTransportServer.start();

0 commit comments

Comments
 (0)