Skip to content

Tomas/connection raii initial#413

Draft
ProfessorTom wants to merge 78 commits intodannagle:developmentfrom
ProfessorTom:tomas/connection-raii-initial
Draft

Tomas/connection raii initial#413
ProfessorTom wants to merge 78 commits intodannagle:developmentfrom
ProfessorTom:tomas/connection-raii-initial

Conversation

@ProfessorTom
Copy link
Contributor

Before submitting a pull request:

  • Did you fork from the development branch? yes
  • Are you submitting the pull request to the development branch? (not master) yes

- New ctor: TCPThread(host, port, initialPacket, parent)
- Creates QSslSocket early
- Connects connected/errorOccurred/stateChanged signals
- Stores host/port for run()
- Sets m_managedByConnection = true
- [[nodiscard]] bool isValid() const
- Checks clientConnection null, error codes, socket state
- Adds detailed qDebug/qWarning logging for failure reasons
- Add null check for clientConnection before calling close()
- Add waitForDisconnected(1000) for clean shutdown when socket exists
- Log warning if called with null socket
- Prevents potential segfault in dtor when socket not initialized
- Add if (clientConnection) guard before close()
- Only call waitForDisconnected() if socket is not UnconnectedState or ClosingState
- Log when wait is skipped or socket is null
- Eliminates Qt warning about waitForDisconnected in UnconnectedState
- Prevents potential null dereference in Connection dtor
…safe cleanup

- Update ctor to pass host/port/initialPacket
- Add public start() method with isValid() check
- Auto-start in ctor
- Add send(), isConnected(), isSecure()
- Forward key TCPThread signals
- Add m_threadStarted flag for safe dtor cleanup
- Add testThreadStartsAndStops() to verify no crash on start/dtor
- Give thread time to start with QTest::qWait(500)
- Check isConnected() (with fallback for Connecting state)
…plication

- Link packet.cpp, tcpthread.cpp, sendpacketbutton.cpp
- Add Qt6::Network for QSslSocket/QTcpSocket
- Switch to per test class QApplication isolation with runGuiTest/runNonGuiTest
Ensure run() uses the correct address/port passed to the managed client constructor,
preventing HostNotFoundError when sendPacket fields are empty.
…ectionLoop

- Check closeRequest/isInterruptionRequested at loop entry
- Add debug output when exiting due to interruption/close request
This helps trace shutdown behavior and prevents unnecessary loop iterations.
Remove duplicate disconnect/close/deleteLater from end of run()'s client path.
Cleanup now happens only once, in the worker thread, when loop exits.
Eliminates race risk and makes shutdown logic clearer.
Replace unsafe clientConnection = &sock (stack variable) with new QSslSocket + setSocketDescriptor.
Prevents dangling pointer when sock goes out of scope.
Adds error handling if setSocketDescriptor fails.
- Set closeRequest and requestInterruption
- Remove direct close/waitForDisconnected calls
- Log caller thread for debugging
This prevents cross-thread socket access crashes when called from main thread.
- Add detailed debug logs with connection ID
- Wait briefly (2s) after signaling thread to stop
- Remove redundant post-flag check (always true)
- Emit disconnected() after wait attempt
Improves traceability without blocking forever.
Include the new lifecycle test file (with QApplication) in CMake
and run it under the GUI/event-loop runner.
Ensures real socket/thread behavior is tested consistently.
Prevents re-entrant close() calls (e.g. signal loops or double clicks).
Initialized to false in header.
Introduces forceShutdown() which sets closeRequest/interruption and calls abort()
on the socket if connected. This safely breaks waitForReadyRead(), waitForConnected(),
etc. from another thread without races. Required for reliable destructor cleanup.
Moves the full sequence (flags → wait → forceShutdown → final wait → logging)
into a reusable helper method. Reduces duplication and makes behavior consistent
between close() and destructor.
…-entrancy guard

- Calls shutdownThreadSafely(2000) for graceful shutdown
- Adds m_isClosing guard to prevent multiple concurrent close calls
- Removes redundant state check after flag reset
- Improves logging with 'initiated' and 'completed' messages
This makes close() safer and easier to trace.
…vely

- Removes call to close() (avoids duplication and long blocks in dtor)
- Only runs if thread is still running (user forgot close())
- Uses shorter timeout (1000 ms) + forceShutdown fallback
- Logs warning when forcing cleanup
This ensures RAII: destructor is fast, safe, and non-blocking.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant