Results 1 to 4 of 4

Thread: Issue with Unix signals and QTestStream readline

  1. #1
    Join Date
    Nov 2023
    Posts
    1
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Issue with Unix signals and QTestStream readline

    Hello I'm trying to implement a signal handler mechanism into my Qt Console application to quit it when a unix signal is catched
    So I used the standard library c function signal in signal.h registering a handler for SIGTERM signal.
    Then I start doing some stuff and in particular I'm reading the stdin using a QTextStream(stdin).readline() method.
    The point is that when I'm waiting for the input on the readline the application does not catch the SIGTERM signal
    Can someone explain me why?

    thank you in adavance

  2. #2
    Join Date
    Nov 2024
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Issue with Unix signals and QTestStream readline

    Your application doesn't catch the `SIGTERM` signal while blocked on `QTextStream(stdin).readLine()` because `readLine()` is a blocking call, preventing the Qt event loop or signal handler from running.

    Solution: Use `QSocketNotifier` to monitor `stdin` asynchronously instead of blocking on `readLine()`, or configure `sigaction` with `SA_RESTART` unset to allow signals to interrupt the blocking call.
    phrazle

  3. #3

    Default Re: Issue with Unix signals and QTestStream readline

    The issue you're encountering arises because when you use QTextStream(stdin).readline() in your Qt console application, it internally calls the standard C function fgets() to read input. While this method is blocking (i.e., it waits for user input), it can also cause the signal handler to not be triggered when the signal is sent (in your case, SIGTERM). This behavior is due to how signals are handled in Unix-like systems and how they interact with blocking system calls like fgets() or QTextStream::readline() Drive Mad Game

  4. #4
    Join Date
    Jan 2025
    Location
    California
    Posts
    1

    Default Re: Issue with Unix signals and QTestStream readline

    Hello

    The issue arises because QTextStream(stdin).readLine() is a blocking call, and signals like SIGTERM are not handled immediately during such operations. The signal handler will execute only after the blocking call completes.

    Solution:
    Use non-blocking I/O with QSocketNotifier to read from stdin asynchronously, allowing signals to be handled promptly:
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QSocketNotifier>
    3. #include <QTextStream>
    4. #include <signal.h>
    5.  
    6. QCoreApplication* app = nullptr;
    7.  
    8. void signalHandler(int) {
    9. if (app) app->quit();
    10. }
    11.  
    12. int main(int argc, char* argv[]) {
    13. QCoreApplication application(argc, argv);
    14. app = &application;
    15.  
    16. signal(SIGTERM, signalHandler); // Register signal handler
    17.  
    18. QSocketNotifier notifier(STDIN_FILENO, QSocketNotifier::Read);
    19. QObject::connect(&notifier, &QSocketNotifier::activated, [&]() {
    20. QTextStream input(stdin);
    21. QString line = input.readLine();
    22. if (!line.isEmpty()) qDebug() << "Input:" << line;
    23. });
    24.  
    25. return application.exec();
    26. }
    To copy to clipboard, switch view to plain text mode 
    This ensures signal handling works even while waiting for input.
    Last edited by d_stranz; 10th January 2025 at 16:47. Reason: missing [code] tags

Similar Threads

  1. Replies: 6
    Last Post: 2nd May 2012, 10:13
  2. qThread and Unix Signals
    By peterjb31 in forum Qt Programming
    Replies: 1
    Last Post: 7th April 2011, 12:24
  3. Performance issue when using signals
    By baluk in forum Newbie
    Replies: 2
    Last Post: 18th November 2010, 05:07
  4. Replies: 7
    Last Post: 29th May 2009, 09:58
  5. QThread and signals (linux/UNIX signals not Qt Signals)
    By Micawber in forum Qt Programming
    Replies: 1
    Last Post: 28th November 2007, 23:18

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.