Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 692531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:39:38+00:00 2026-05-14T02:39:38+00:00

OK this is presumably a hard one, I’ve got an pyGTK application that has

  • 0

OK this is presumably a hard one, I’ve got an pyGTK application that has random crashes due to X Window errors that I can’t catch/control.

So I created a wrapper that restarts the app as soon as it detects a crash, now comes the problem, when the user logs out or shuts down the system, the app exits with status 1. But on some X errors it does so too.

So I tried literally anything to catch the shutdown/logout, with no success, here’s what I’ve tried:

import pygtk
import gtk
import sys


class Test(gtk.Window):
    def delete_event(self, widget, event, data=None):
        open("delete_event", "wb")

    def destroy_event(self, widget, data=None):
        open("destroy_event", "wb")

    def destroy_event2(self, widget, event, data=None):
        open("destroy_event2", "wb")

    def __init__(self):
        gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL)
        self.show()
        self.connect("delete_event", self.delete_event)
        self.connect("destroy", self.destroy_event)
        self.connect("destroy-event", self.destroy_event2)      

def foo():
    open("add_event", "wb")

def ex():
    open("sys_event", "wb")


from signal import *
def clean(sig):
    f = open("sig_event", "wb")
    f.write(str(sig))
    f.close()
    exit(0)

for sig in (SIGABRT, SIGILL, SIGINT, SIGSEGV, SIGTERM):
    signal(sig, lambda *args: clean(sig))


def at():
    open("at_event", "wb")

import atexit
atexit.register(at)

f = Test()
sys.exitfunc = ex
gtk.quit_add(gtk.main_level(), foo)

gtk.main()
open("exit_event", "wb")

Not one of these succeeds, is there any low level way to detect the system shutdown? Google didn’t find anything related to that.

I guess there must be a way, am I right? :/

EDIT:
OK, more stuff.

I’ve created this shell script:

#!/bin/bash


trap test_term TERM
trap test_hup HUP


test_term(){
    echo "teeeeeeeeeerm" >~/Desktop/term.info
    exit 0
}

test_hup(){
    echo "huuuuuuuuuuup" >~/Desktop/hup.info
    exit 1
}

while [ true ]
do
    echo "idle..."
    sleep 2
done

And also created a .desktop file to run it:

[Desktop Entry]
Name=Kittens
GenericName=Kittens
Comment=Kitten Script
Exec=kittens
StartupNotify=true
Terminal=false
Encoding=UTF-8
Type=Application
Categories=Network;GTK;
Name[de_DE]=Kittens 

Normally this should create the term file on logout and the hup file when it has been started with &. But not on my System. GDM doesn’t care about the script at all, when I relog, it’s still running.

I’ve also tried using shopt -s huponexit, with no success.

EDIT2:
Also here’s some more information aboute the real code, the whole thing looks like this:

Wrapper Script, that catches errors and restarts the programm
    -> Main Programm with GTK Mainloop
        -> Background Updater Thread

The flow is like this:

Start Wrapper
-> enter restart loop
    while restarts < max:
        -> start program
            -> check return code
                -> write error to file or exit the wrapper on 0

Now on shutdown, start program return 1. That means either it did hanup or the parent process terminated, the main problem is to figure out which of these two did just happen.
X Errors result in a 1 too. Trapping in the shellscript doesn’t work.

If you want to take a look at the actual code check it out over at GitHub:
http://github.com/BonsaiDen/Atarashii

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:39:38+00:00Added an answer on May 14, 2026 at 2:39 am

    OK, I finally found the solution 🙂

    You simply can’t rely on signals in this case. You have to connect to the Desktop Session in order to get notified that a logout is going to happen.

    import gnome.ui
    
    gnome.program_init('Program', self.version) # This is going to trigger a warning that program name has been set twice, you can ignore this, it seems to be a problem with a recent version of glib, the warning is all over the place out there
    client = gnome.ui.master_client() # connect us to gnome session manager, we need to init the program before this
    client.connect('save-yourself', self.on_logout) # This gets called when the user confirms the logout/shutdown
    client.connect('shutdown-cancelled', self.on_logout_cancel) # This gets called when the logout/shutdown is canceled
    client.connect('die', self.on_logout) # Don't know when this gets called it never got in my tests
    
    def on_logout(self, *args):
        # save settings an create a file that tells the wrapper that we have exited correctly!
        # we'll still return with status code 1, but that's just gtk crashing somehow
    
    def on_logout_cancel(self, *args):
        # simply delete the logout file if it exists
    

    One important note here: Don’t try to exit your program in on_logout, if you do so, GNOME won’t recognize that your program has been exited and will give you the dialog that some programs are still running.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In this question it was explained that std::for_each has undefined behavior when given an
According to a section in this presumably accurate book , A common use of
In this blog article they use the construct: @measured def some_func(): #... # Presumably
Currently I have some code that looks like this void calc_run(Calculation *c, Input *i);
It's hard to explain our situaction. We have a 3-tier application. The engine is
I have a model that looks something like this: public class SampleModel { public
This question is related to a previous one on writing a session timeout handler
I'm working with COGNOS, a very frustrating BI application that relies heavily on Javascript.
this is really two questions in one I guess. We've developed a .Net app
This question concerns XML schemas and files. Suppose I am developing a desktop application

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.