django-background-tasks icon indicating copy to clipboard operation
django-background-tasks copied to clipboard

Opening admin page causes process_tasks to exit silently

Open damien-king opened this issue 6 years ago • 12 comments

Hi,

Firstly, thanks for all your hard work on this excellent package!

I have a strange issue that I'm at a loss on how to troubleshoot.

  • manage.py process_tasks will begin running a task or two as per usual (multiple or single queue doesn't matter).
  • If I then open the /background_task/tasks admin page, any running process silently closes the second I load the page, leaving the tasks marked as locked by a process that is no longer running (process_tasks just exists cleanly and I can see the python process(es) stop running).

This would be fine if it resumed afterwards, but if I run process_tasks it see's 2 items are already locked and doesn't pickup any new tasks, I have to manually change the locked at and locked time fields for it to start processing again.

Any thoughts on how to troubleshoot this would be greatly appreciated!

Thanks.

damien-king avatar Jun 15 '18 00:06 damien-king

I had the same problem too, when I open the completed_tasks in admin page the process terminates. Since I don't need the completed tasks, I just deleted it in the library and that solved the problem for me.

cmpss92140 avatar Jul 02 '18 14:07 cmpss92140

Thank you @damienmp for reporting this issue. I cannot reproduce this. Could you give more info (database, versions, ..)?

For the rest of the issue, jap, this is known: https://github.com/arteria/django-background-tasks/issues/151

philippeowagner avatar Jul 12 '18 10:07 philippeowagner

Hi @philippeowagner,

Sorry should have included in the first place...

Django==1.10.3 django-background-tasks==1.1.13 Postgres ==9.6

All running on Windows Server 2012 R2

settings.py BACKGROUND_TASK_RUN_ASYNC = True BACKGROUND_TASK_ASYNC_THREADS = 2

All other settings are as per default. So with #151, if I remove the async and async threads, will it still lock? If not then that would be a sufficient workaround for this whole issue for me :)

Thanks for your time.

damien-king avatar Jul 18 '18 04:07 damien-king

@cmpss92140 are you using Windows as well?

philippeowagner avatar Oct 18 '18 08:10 philippeowagner

This is happening consistently in my Windows development environment. If there is a completed task in the task queue, as soon as the admin page for that task is displayed, the worker task ends with exit 0. I have tried to debug this without success. Fortunately I deploy to a linux environment where it works fine Windows 10,0,17134, Django 2.1.2 django-background-tasks 1.2.0

ianastewart avatar Dec 04 '18 08:12 ianastewart

I am able to reproduce this consistently on my Windows environment. locked_by_pid_running in the Task model uses os.kill to check if the process is running. On Windows, os.kill(pid, 0) is the same as os.kill(pid, signal.CTRL_C_EVENT) which terminates the process.

I have instead used psutils which is a cross-platform library to check if pid exists and done some monkey patching to replace the method. Works perfectly fine..

vyshnavimudumby avatar Jun 21 '19 05:06 vyshnavimudumby

Same problem happens to me with Django 2.2.2, django-backdround-tasks 1.2.0 Windows 10. Will there be an official fix on that or shall we dive into workarounds?

MiliasV avatar Jun 27 '19 10:06 MiliasV

This is goofy...

Thanks @vyshnavimudumby for the suggestion.

I will merge a PR resolving the Windows related issues.

philippeowagner avatar Jul 01 '19 08:07 philippeowagner

Can confirm:

from psutil import pid_exists

def locked_by_pid_running(self):
        """
        Check if the locked_by process is still running.
        """
        if self.locked_by:
            if pid_exists(int(self.locked_by)):
                return True
            else:
                return False
        else:
            return None

Fixes the issue

damien-king avatar Aug 08 '19 01:08 damien-king

@damien-king where did you put this fix? is it in background_task/models_completed.py or background_task/models.py. And is this fix for when viewing tasks or completed tasks in admin view? I am having the same issue for awhile now and haven't been able to solve it.

UPDATE: I realized that one is for the tasks view and the other is for the completed_tasks view in admin. This sorted the issue for me too but are there any repercussions to using this fix? If not then I can confirm that your fix did solve the issue and if we can get it updated on the repo.

Moon-developer avatar Sep 05 '19 07:09 Moon-developer

@philippeowagner Did you ever had the chance to merge the PR? I am asking because the behavior is still the same as of today.

ViRuSTriNiTy avatar Mar 10 '23 18:03 ViRuSTriNiTy

Same behaviour still exists. Accessing the admin page for tasks (not completed tasks) causes process_tasks to silently stop.

This was a tough one to debug. The only error log I could find was a warning from the database in Windows logs "Aborted connection 105967 to db: 'mydb' user: 'username' host: 'localhost' (Got an error reading communication packets)"

django-background-tasks 1.2.5 Windows Server 2019

mrblg avatar Sep 13 '24 08:09 mrblg