Skip to content

Join the socket thread before deleting it in ~Socket() - #168

Open
quix2k23 wants to merge 1 commit into
Ultimaker:mainfrom
quix2k23:fix-socket-destructor-join
Open

quix2k23 wants to merge 1 commit into
Ultimaker:mainfrom
quix2k23:fix-socket-destructor-join

Conversation

@quix2k23

Copy link
Copy Markdown

Problem

Socket::~Socket() can call std::terminate. CuraEngine then aborts at exit with:

terminate called without an active exception

Stack (CuraEngine 5.14.0 on Linux):

std::terminate
Arcus::Socket::~Socket()          (libArcus.so)
cura::ArcusCommunication::~ArcusCommunication()
cura::Application::~Application()

When the socket is already Closed or Error (for example because Cura closed the connection first), close() takes its early-return branch and does not join the worker thread. The destructor then runs delete d->thread on a thread that is still joinable, and std::thread::~thread() calls std::terminate. The worker loop has already exited in that state, so joining returns immediately.

On Linux the abort shows up as a crash report for CuraEngine (for example in ABRT on Fedora), even though the slice itself completed.

Fix

Join the thread (if joinable) before deleting it in the destructor, and reset the pointer.

How I tested

A small program that reproduces it: connect an Arcus::Socket to a plain TCP listener, close the accepted connection from the listener side, wait until the socket reports Closed/Error, then delete the socket.

  • Before: aborts with terminate called without an active exception (exit 134).
  • After: exits cleanly (5 of 5 runs).
Repro program
#include <Arcus/Socket.h>
#include <arpa/inet.h>
#include <chrono>
#include <cstdio>
#include <netinet/in.h>
#include <sys/socket.h>
#include <thread>
#include <unistd.h>

int main()
{
    int srv = socket(AF_INET, SOCK_STREAM, 0);
    sockaddr_in a{};
    a.sin_family = AF_INET;
    a.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
    if (bind(srv, (sockaddr*)&a, sizeof a) || listen(srv, 1)) { perror("bind/listen"); return 2; }
    socklen_t l = sizeof a;
    getsockname(srv, (sockaddr*)&a, &l);

    auto* s = new Arcus::Socket();
    s->connect("127.0.0.1", ntohs(a.sin_port));
    close(accept(srv, nullptr, nullptr)); // peer closes first

    for (int i = 0; i < 100; ++i)
    {
        auto st = s->getState();
        if (st == Arcus::SocketState::Closed || st == Arcus::SocketState::Error) break;
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }
    delete s; // std::terminate without the fix
    puts("OK");
}

Side note (not changed here)

The condition just above, d->state != SocketState::Closed || d->state != SocketState::Error, is always true, so close() is always called. That is harmless with this fix, so I left it alone to keep the change minimal. && was probably intended.

When the connection was already closed or errored (for example because the
peer closed first), close() returns early without joining the worker thread.
The destructor then deleted a joinable std::thread, which calls
std::terminate. CuraEngine aborts at exit with
"terminate called without an active exception".
@github-actions github-actions Bot added the PR: Community Contribution 👑 Community Contribution PR's label Nov 21, 2026
quix2k23 added a commit to quix2k23/cura-inner-outer-inner that referenced this pull request Nov 21, 2026
UltiMaker's libArcus deletes a still-joinable thread in Socket::~Socket()
when Cura closed the connection first, so CuraEngine aborted at exit
(terminate called without an active exception). The release build now
exports a patched arcus recipe (scripts/patch-arcus.py, fix offered
upstream as Ultimaker/libArcus#168), checks that the engine resolves to
it, and on Linux runs a repro test against the packaged library.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PR: Community Contribution 👑 Community Contribution PR's

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant