unitaryfund / metriq-gym

Standard benchmark script implementations for https://metriq.info
Apache License 2.0
2 stars 0 forks source link

Make JobManager thread-safe #51

Open vprusso opened 1 day ago

vprusso commented 1 day ago

An instance of the JobManager class is not thread-safe. This can lead to race conditions, deadlocks, etc.

We can do this by making using of the threading module in Python. From there, we would add a lock variable to the JobManager class:

class JobManager:
    def __init__(self, file_path: str = DEFAULT_FILE_PATH):
        self.file_path = file_path
        self.lock = threading.Lock() 
        self._load_jobs()

And any time we save, add, remove, etc. jobs, we want to use the with construct to invoke and use the lock. For instance:

    def remove_job(self, job_id: str):
        """Removes a job by its ID."""
        with self.lock:
            self.jobs = [job for job in self.jobs if job["id"] != job_id]
            self._save_jobs()

etc.