Results 1 to 5 of 5

Thread: Issue with Unix signals and QTestStream readline

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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
    Dec 2023
    Posts
    1
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Issue with Unix signals and QTestStream readline

    Quote Originally Posted by ulix View Post
    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 uno online 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
    As I know, QApplication does not handle SIGTERM signal and exit gracefully by default. You may need to manually handle the signal by connecting it to a slot that calls QCoreApplication::quit() to exit the application. Here’s an example of how you can handle the SIGTERM signal in your application:

    Qt Code:
    1. #include <QCoreApplication>
    2. #include <signal.h>
    3.  
    4. void signalHandler(int signalNumber)
    5. {
    6. if (signalNumber == SIGTERM) {
    7. }
    8. }
    9.  
    10. int main(int argc, char *argv[])
    11. {
    12. QCoreApplication app(argc, argv);
    13.  
    14. // Register the signal handler for SIGTERM
    15. signal(SIGTERM, signalHandler);
    16.  
    17. // Do some stuff here
    18. QTextStream(stdin).readLine();
    19.  
    20. return app.exec();
    21. }
    To copy to clipboard, switch view to plain text mode 
    This code registers a signal handler for SIGTERM using the signal() function from signal.h. When the SIGTERM signal is caught, the signalHandler() function is called, which calls QCoreApplication::quit() to exit the application gracefully. I hope this helps! Let me know if you have any other questions.

  3. #3
    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

  4. #4

    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

  5. #5
    Join Date
    Jan 2025
    Location
    California
    Posts
    2

    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
  •  
Qt is a trademark of The Qt Company.