Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Stowarzyszenie-Talent/sioworkers
Browse files Browse the repository at this point in the history
  • Loading branch information
otargowski committed Mar 10, 2023
2 parents 5cc1b7f + f3dda53 commit ca037e3
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions sio/workers/sandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import fcntl
import os.path
from hashlib import sha1
from threading import Lock
import time
import tarfile
import shutil
Expand Down Expand Up @@ -154,24 +155,28 @@ def __init__(self, name):

self.path = os.path.join(SANDBOXES_BASEDIR, name)
_mkdir(SANDBOXES_BASEDIR)

# This is needed for safe operation with multiple threads,
# as file locking is useless for this.
self.local_lock = Lock()
self._in_context = 0

def __enter__(self):
self._in_context += 1
if self._in_context == 1:
try:
self.lock = _FileLock(self.path + '.lock')
self._get()
except:
self._in_context -= 1
raise
with self.local_lock:
self._in_context += 1
if self._in_context == 1:
try:
self.file_lock = _FileLock(self.path + '.lock')
self._get()
except:
self._in_context -= 1
raise
return self

def __exit__(self, exc_type, exc_value, traceback):
self._in_context -= 1
if self._in_context == 0:
self.lock.unlock()
with self.local_lock:
self._in_context -= 1
if self._in_context == 0:
self.file_lock.unlock()

def __str__(self):
return "<Sandbox: %s at %s>" % (
Expand Down Expand Up @@ -295,18 +300,18 @@ def _get(self):

logger.debug("Sandbox '%s' requested", name)

self.lock.lock_shared()
self.file_lock.lock_shared()

if not self._should_install_sandbox():
# Sandbox is ready, so we return and *maintain* the lock
# Sandbox is ready, so we return and *maintain* the file lock
# for the lifetime of this object.
return

self.lock.unlock()
self.lock.lock_exclusive()
self.file_lock.unlock()
self.file_lock.lock_exclusive()

if not self._should_install_sandbox():
self.lock.lock_shared()
self.file_lock.lock_shared()
return

try:
Expand Down Expand Up @@ -362,10 +367,10 @@ def _get(self):
logger.info(" done.")

except:
self.lock.unlock()
self.file_lock.unlock()
raise

self.lock.lock_shared()
self.file_lock.lock_shared()


def get_sandbox(name):
Expand Down

0 comments on commit ca037e3

Please sign in to comment.