Repository navigation
Conversation
metaRTC seeds yang_random() almost only with the pid, and inside a Flatpak sandbox every instance has the same pid, so every CrealityPrint window drew the same local WebRTC port. When a second window opened the liveview, its bind() failed and metaRTC called exit(1), which aborted in a static destructor (UpdateParams' joinable thread) with SIGABRT. Ask the OS for a free UDP port right before playRtc() and use it as the connection's local port. If the probe fails, keep the previous behaviour. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012y9SfdL6ZXB2uijJgTbY7Q
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.
Description
Summary
On Linux, opening the printer camera (liveview) in a second CrealityPrint window, while another window already has it open, closed the second window immediately with
SIGABRT. It happened every time with the Flatpak build. The cause is the local UDP port that metaRTC picks for the WebRTC connection: inside a Flatpak sandbox, every instance picks the same one. This PR makesWebRTCDecoderask the OS for a free UDP port before each connection.One file is changed:
src/video/WebRTCDecoder.cpp(+23 lines). Windows and macOS are not affected in practice:WebRTCDecoder::startPlay()is only called on Linux (HttpServer.cpp, under#if defined(__linux__)).Symptom
Open CrealityPrint, connect to a printer (here a Creality Hi at
192.168.100.30) and open the camera view.Open a second CrealityPrint window, for example by double-clicking a
.3mf(single_instanceis off), and open the camera view there.The second window disappears. stderr shows:
and
systemd-coredumprecords aSIGABRTfor/app/CrealityPrint. The first window keeps working.Root cause
The crash is a chain of four steps:
Same "random" port in every instance.
WebRTCDecoder::WebRTCDecoder()setsavinfo.rtc.rtcLocalPort = 10000 + yang_random() % 15000.YangRtcReceive::init()then uses that value as the local port of the connection (streamconfig.localPort = m_context->avinfo.rtc.rtcLocalPort++). metaRTC seedsyang_random()once, withsrandom(yang_update_system_time() | (getpid() << 13))(YangMath.c).yang_update_system_timeis a macro foryang_get_system_micro_time(), which counts microseconds since its own first call, so it is close to 0 on that first call. The seed is therefore almost only the pid. TheWebRTCDecodersingleton is constructed during static initialization, so the call always happens at the same point of startup.Inside a Flatpak sandbox, the app's pid is the same in every instance (the main thread is LWP 2 in the core dumps). Every instance therefore draws the same port. Here that was UDP
12171, whichss -uanpshowed held by the first instance.metaRTC exits the process when
bind()fails. The UDP receive thread does:(
YangRtcSocket.c:75)exit()runs static destructors while other threads are alive. One of them is the singleton inUpdateParams::getInstance()(static UpdateParams instance;). Itsstd::thread m_check_threadis still joinable, sostd::thread::~thread()callsstd::terminate().std::terminate()without an active exception printsterminate called without an active exceptionand aborts.Fix
src/video/WebRTCDecoder.cpp:find_free_udp_port()(anonymous namespace). It opens aboost::asioUDP socket bound to port 0 on IPv4, reads the port the kernel assigned, and closes the socket. If the probe fails it returns 0.WebRTCDecoder::startPlay()calls it right beforem_player->playRtc()and, when it returns a port, stores it inm_context->avinfo.rtc.rtcLocalPort. This is the valueYangRtcReceive::init()copies into the connection'slocalPort.Why this approach:
deps/metartc, tagv1.0.0). The WHEP path that the liveview uses (connectWhipWhepServer→yang_whip_connectPeer) does not overridelocalPort. The other path that randomizes it (yang_stream_parseUrl) is not used byYangRtcReceive::init(puid, url).startPlay(), so reconnecting afterstopPlay()also gets a free port.The window between closing the probe socket and metaRTC binding the port is a few milliseconds. Another process would have to grab exactly that port in that window, which is far less likely than the old guaranteed collision.
Not changed (possible follow-ups)
exit(1)on a UDP bind failure, and in several other places (YangRtcSocket.c,YangCTimer.c, decoders, audio). Any such failure still closes the app. Returning an error fromyang_run_rtcudp_threadwould need a patch to the metaRTC dependency.UpdateParams(and possibly other singletons) still hold a joinablestd::threadat static destruction. Anexit()from any thread therefore turns intoSIGABRTinstead of a normal exit.yang_random()seed stays weak (pid plus microseconds since first call). It is also used for ICE credentials (yang_cstr_random).Screenshots/Recordings/Graphs
No UI change. Backtrace of the crashing thread, from the core dump of the installed (stripped) build, with frames 6 and 9 resolved against a non-stripped build of the same sources. Both binaries have identical segment layout.
The code right before frame 9's return address is
call yang_clog(the "Udp server bind error" message) followed bymov $0x1,%edi; call exit@plt.Tests
Tested on Fedora 45, GNOME 50, Wayland, Flatpak (GNOME 50 runtime, LLVM 21), with a Creality Hi on the local network:
🤖 Generated with Claude Code