Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't start killed process #12

Closed
coffebar opened this issue Sep 14, 2014 · 7 comments
Closed

Can't start killed process #12

coffebar opened this issue Sep 14, 2014 · 7 comments

Comments

@coffebar
Copy link

When I killed daemon process "python ShowMe.py" from OS,
I am triyng to start it again

python ShowMe.py start

and getting error

daemon.pid already exist. Cannot start daemon.

but process is dead!

so, I changed in start method

if pid is not None:
    message = "Pidfile %s already exist. Cannot start daemon."

to

if pid is not None and psutil.pid_exists(pid):
    message = "Pidfile %s already exist. Cannot start daemon."

psutil imported from https://github.com/giampaolo/psutil

@kevinconway
Copy link
Owner

What signal did you use to kill the process?

@coffebar
Copy link
Author

SIGKILL

@kevinconway
Copy link
Owner

Ah, yeah. SIGKILL terminates the process without allowing it to run the cleanup logic. It's a known shortcoming of using signal trapping to perform pidfile cleanup. I think something like psutils would be a good alternative. I'm trying to keep the project dependency free so I will likely make psutils an optional driver for pid management.

Is using SIGKILL a requirement for your project? If not you should consider using another signal such as SIGTERM as it will properly tigger the cleanup logic and remove the pidfile.

@coffebar
Copy link
Author

Ah, yeah. SIGKILL terminates the process without allowing it to run the cleanup logic

it's not a secret

I'm trying to keep the project dependency free so I will likely make psutils an optional driver for pid management.

it would be great

Is using SIGKILL a requirement for your project?

I think that this is possible as the external impact

@tenortim
Copy link

The problem isn't just if the daemon is killed. The same issue happens if it dies unexpectedly. Or the host system crashes or loses power. Or any number of other possibilities. That is why full pidfile handling needs to test that the pid is valid.

I need to test portability of the method, but on POSIX systems, there is no need for external dependencies. It's as easy as something like

try:
    os.kill(pid, 0)
except OSError as e:
    if e.errno == errno.EPERM:
        print("pid %d is running under a different username" % pid)
    elif e.errno != errno.ESRCH:
        print("kill of pid %d failed with error %s" % (pid, e.strerror))
    else:
        print("pid %d is running" % pid)

Windows supports os.kill in 2.7 onwards. I'll see if it works there.

@tenortim
Copy link

The code as it stands doesn't work on Windows for a number of unrelated reasons.
The signal handling needs SIGQUIT removed since it doesn't exist on Windows (trivial), but then daemonize fails because os.fork doesn't exist either.

As such, I already have a fix (plus tests) for the pidfile handling in my clone. I'll submit a pull request.

@kevinconway
Copy link
Owner

Thanks to @tenortim I can finally close this bug!

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

No branches or pull requests

3 participants