Skip to content

Commit 40ec8af

Browse files
committed
Bug 1663718 - Don't put too much data in buffer when the data can't be written to socket r=dragana
The main reason we used too much memory is that we ignore the `NS_BASE_STREAM_WOULD_BLOCK` returned from socket output stream. When the output stream is blocked, all the data is stored in the output queue and make the memory usage high. Differential Revision: https://phabricator.services.mozilla.com/D89563
1 parent 11b8b5a commit 40ec8af

File tree

5 files changed

+33
-1
lines changed

5 files changed

+33
-1
lines changed

netwerk/protocol/websocket/WebSocketChannel.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3862,6 +3862,10 @@ void WebSocketChannel::DoEnqueueOutgoingMessage() {
38623862
mHdrOut, mHdrOutSize, (uint8_t*)mCurrentOut->BeginReading(),
38633863
mCurrentOut->Length());
38643864

3865+
if (rv == NS_BASE_STREAM_WOULD_BLOCK) {
3866+
return;
3867+
}
3868+
38653869
LOG(("WebSocketChannel::DoEnqueueOutgoingMessage: rv %" PRIx32 "\n",
38663870
static_cast<uint32_t>(rv)));
38673871

@@ -3924,6 +3928,12 @@ WebSocketChannel::OnDataReceived(uint8_t* aData, uint32_t aCount) {
39243928
return ProcessInput(aData, aCount);
39253929
}
39263930

3931+
NS_IMETHODIMP
3932+
WebSocketChannel::OnReadyToSendData() {
3933+
DoEnqueueOutgoingMessage();
3934+
return NS_OK;
3935+
}
3936+
39273937
} // namespace net
39283938
} // namespace mozilla
39293939

netwerk/protocol/websocket/WebSocketConnectionChild.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,12 @@ WebSocketConnectionChild::OnDataReceived(uint8_t* aData, uint32_t aCount) {
174174
return NS_OK;
175175
}
176176

177+
NS_IMETHODIMP
178+
WebSocketConnectionChild::OnReadyToSendData() {
179+
// TODO: implement flow control between parent and socket process.
180+
return NS_OK;
181+
}
182+
177183
void WebSocketConnectionChild::ActorDestroy(ActorDestroyReason aWhy) {
178184
LOG(("WebSocketConnectionChild::ActorDestroy %p\n", this));
179185
if (mConnection) {

netwerk/protocol/websocket/nsIWebSocketConnection.idl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,9 @@ interface nsIWebSocketConnectionListener : nsISupports
7171

7272
void onDataReceived([array, size_is(dataLength)]in uint8_t data,
7373
in unsigned long dataLength);
74+
75+
/**
76+
* Called to inform the listener that the outgoing data is ready to write.
77+
*/
78+
void onReadyToSendData();
7479
};

netwerk/protocol/websocket/nsWebSocketConnection.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ nsWebSocketConnection::nsWebSocketConnection(
2020
mSocketIn(aInputStream),
2121
mSocketOut(aOutputStream),
2222
mWriteOffset(0),
23-
mStartReadingCalled(false) {
23+
mStartReadingCalled(false),
24+
mOutputStreamBlocked(false) {
2425
LOG(("nsWebSocketConnection ctor %p\n", this));
2526
}
2627

@@ -105,6 +106,10 @@ nsWebSocketConnection::EnqueueOutputData(const uint8_t* aHdrBuf,
105106
mSocketOut->AsyncWait(this, 0, 0, mEventTarget);
106107
}
107108

109+
if (mOutputStreamBlocked) {
110+
return NS_BASE_STREAM_WOULD_BLOCK;
111+
}
112+
108113
return NS_OK;
109114
}
110115

@@ -215,6 +220,8 @@ nsWebSocketConnection::OnOutputStreamReady(nsIAsyncOutputStream* aStream) {
215220
return NS_OK;
216221
}
217222

223+
mOutputStreamBlocked = false;
224+
218225
while (!mOutputQueue.empty()) {
219226
const OutputData& data = mOutputQueue.front();
220227

@@ -230,6 +237,7 @@ nsWebSocketConnection::OnOutputStreamReady(nsIAsyncOutputStream* aStream) {
230237

231238
if (rv == NS_BASE_STREAM_WOULD_BLOCK) {
232239
mSocketOut->AsyncWait(this, 0, 0, mEventTarget);
240+
mOutputStreamBlocked = true;
233241
return NS_OK;
234242
}
235243

@@ -248,5 +256,7 @@ nsWebSocketConnection::OnOutputStreamReady(nsIAsyncOutputStream* aStream) {
248256
}
249257
}
250258

259+
Unused << mListener->OnReadyToSendData();
260+
251261
return NS_OK;
252262
}

netwerk/protocol/websocket/nsWebSocketConnection.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class nsWebSocketConnection : public nsIWebSocketConnection,
6060
size_t mWriteOffset;
6161
std::list<OutputData> mOutputQueue;
6262
bool mStartReadingCalled;
63+
bool mOutputStreamBlocked;
6364
};
6465

6566
} // namespace net

0 commit comments

Comments
 (0)