IS-AgroSmart / AgroSmart-Web

1 stars 1 forks source link

Server may get stuck on a weird directory #139

Closed jreyesr closed 3 years ago

jreyesr commented 3 years ago

Some operations change the working dir of the Python server, to ease the execution of os.system calls. For an example, see below:

https://github.com/IS-AgroSmart/AgroSmart-Web/blob/bef44a9357d1f3adea7ae067489fad11dd3548df/core/models.py#L270-L282

This means that, if an exception is thrown between the two calls to os.chdir (most likely on the os.system call), the script will be left on that temporary directory. This is an inconsistent state, since parts of the code assume the current directory to be ROOT_DIR/app, where ROOT_DIR is the directory where AgroSmart-Top was cloned. An example of this type of code would be the code that serves Flight thumbnails:

https://github.com/IS-AgroSmart/AgroSmart-Web/blob/b8e38c7e90738ffa7b96edb7a480f340dd28f9d0/core/views.py#L354-L355

Proposed solution: implement a context manager to automatically restore the current working dir, even if the wrapped code raises an exception.

jreyesr commented 3 years ago

Required changes: