fix: [viewer] make RemoteServer message handling connection-safe - #10308
Open
carlos-al wants to merge 1 commit into
Open
fix: [viewer] make RemoteServer message handling connection-safe#10308carlos-al wants to merge 1 commit into
carlos-al wants to merge 1 commit into
Conversation
z3moon
requested changes
Aug 18, 2026
| } | ||
|
|
||
| char const * RemoteServer::peekIncomingLabel() const { | ||
| static thread_local std::string label; |
Contributor
There was a problem hiding this comment.
this static thread_local causes another problem, which is the returned value could be changed as the same thread calls peekIncomingLabel() again when a new message just arrives.
I see this function is used by one thread at this moment, so technically the current impl might be safe for now. But it's not recommended to use thread_local to just to store data. I would make it return std::string or make this function to take a caller-supplied buffer.
| return true; | ||
| } | ||
|
|
||
| std::lock_guard lock(mConnectionsMutex); |
Contributor
There was a problem hiding this comment.
this lock looks to serialize too many things (insert, new, memcpy,e tc), which seems unnecessary. could you scope this to just mConnections then process the rest outside of it?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes two memory-safety issues in
RemoteServermessage handling:The first allowed callbacks from different CivetWeb workers to invalidate shared vector storage. The second allowed the queued message to change after Java selected its allocation and copy length.
Analysis
Concurrent WebSocket assembly
MessageReceiverstored fragment state in a singlemChunkvector andmReceivedMessagepointer.CivetWeb can invoke the same receiver concurrently for different
mg_connectionobjects. Interleaved fragmented messages could therefore splice state from unrelated clients or reallocate shared vector storage while another worker wasusing it.
The missing-defense regression reproduces this as an ASan memory error across two CivetWeb workers.
Android message acquisition
Android previously:
ByteBuffer.A same-label replacement between those operations could make JNI copy the stale length from a different, shorter message.
The Fix
Track fragment assembly independently for each
mg_connectionand serialize access to that state.Closing a connection removes only its partial message, and server teardown drains remaining partial state after CivetWeb workers stop.
Replace Android’s multi-call protocol with one JNI acquisition operation. JNI now pops one message, validates its actual size, allocates a matching direct buffer, copies exactly that message, and assigns its label and buffer to the
same Java result object.
The public Java
acquireReceivedMessage()API remains unchanged.Testing
Added six loopback WebSocket regressions covering:
Additional verification: