Windows Task Hangs Due to sys.exit Call

1833
2
Jump to solution
03-11-2021 10:59 AM
NathanHeickLACSD
Occasional Contributor III

We just migrated Python scripts running on an old Windows 7 VM to a Windows 10 VM.  Most of them worked just fine, but one script that ends with the following statement hangs when run from Task Scheduler.

 

 

sys.exit(0 if successful else 1)

 

 

If I run it from the command line or IDLE, it completes just fine.  It only hangs as a scheduled task.  I added print statements on every line at the end and the only one that doesn't execute is the one after sys.exit.  So either it is hanging on sys.exit in Python or Task Scheduler is having an issue after the script finishes.  This only happens in the script where I have a sys.exit call.  I added it to let me know if the script had any errors in it.  I catch any exceptions in the script and add them to an email notification, so the script normally ends with return code 0 if I don't do this.  I can imagine some workarounds, but I wanted to see why it no longer works anymore.  Any one else have experience with this?

0 Kudos
1 Solution

Accepted Solutions
NathanHeickLACSD
Occasional Contributor III

After a lot of troubleshooting, I was able to determine that if I comment out a function call to arcpy.management.SelectLayerByLocation, the issue went away.  So, I made a simple test script where I just created a feature layer and performed a selection by location and replicated it very clearly.  It turns out that the problem did not occur on any other machine.  So, we recreated the VM and the problem went away.  In the end, this had nothing to do with sys.exit.  It was just the last line in the script.  The issue was that this one function call would cause the script not to exit only on this machine and only from the command prompt or as a scheduled task.  I hope this helps someone else.

View solution in original post

2 Replies
NathanHeickLACSD
Occasional Contributor III

I'm diligently troubleshooting now.  The main block of code in the script is a try/except/finally block.  If I replace the try with pass, the script exits successfully as a task.  So, it seems like something from inside the try block is keeping it from exiting.  What's interesting is that the task is set to stop the task if it runs over two hours and kill it if it won't stop on its own.  That is not working at all.  So, I am going through the try block a little bit at a time and running the script.  I will let you know if I solve it.

0 Kudos
NathanHeickLACSD
Occasional Contributor III

After a lot of troubleshooting, I was able to determine that if I comment out a function call to arcpy.management.SelectLayerByLocation, the issue went away.  So, I made a simple test script where I just created a feature layer and performed a selection by location and replicated it very clearly.  It turns out that the problem did not occur on any other machine.  So, we recreated the VM and the problem went away.  In the end, this had nothing to do with sys.exit.  It was just the last line in the script.  The issue was that this one function call would cause the script not to exit only on this machine and only from the command prompt or as a scheduled task.  I hope this helps someone else.