Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix race condition #1445

Merged
merged 20 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 14 additions & 6 deletions .github/actions/pytest_run/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,27 @@ runs:
- name: Test with pytest
run: |
which python
python -m pytest -n auto -rXx -v -m "not (parallel or xdist_incompatible) and c ${{ inputs.pytest_mark }}" --ignore=symbolic --ignore=ndarrays
if [ -n "${{ runner.debug }}" ]
then
# If running in debug mode
export "DEBUG=1"
export FLAGS="-xsv --log-cli-level DEBUG"
else
export FLAGS=""
fi
python -m pytest -n auto -rXx ${FLAGS} -m "not (parallel or xdist_incompatible) and c ${{ inputs.pytest_mark }}" --ignore=symbolic --ignore=ndarrays
if [ -n "${SITE_DIR}" ]; then
echo "Touching"
# Test ndarray folder update (requires parallel tests to avoid clean)
touch ${SITE_DIR}/pyccel/stdlib/cwrapper/cwrapper.h
python -m pytest -n auto -rXx -v -m c -k test_array_int32_1d_scalar epyccel/test_arrays.py
python -m pytest -n auto -rXx ${FLAGS} -m c -k test_array_int32_1d_scalar epyccel/test_arrays.py
fi
python -m pytest -rXx -m "xdist_incompatible and not parallel and c ${{ inputs.pytest_mark }}" --ignore=symbolic --ignore=ndarrays
python -m pytest -rXx ${FLAGS} -m "xdist_incompatible and not parallel and c ${{ inputs.pytest_mark }}" --ignore=symbolic --ignore=ndarrays
pyccel-clean
python -m pytest -n auto -rXx -m "not (parallel or xdist_incompatible) and not (c or python) ${{ inputs.pytest_mark }}" --ignore=symbolic --ignore=ndarrays
python -m pytest -rXx -m "xdist_incompatible and not parallel and not (c or python) ${{ inputs.pytest_mark }}" --ignore=symbolic --ignore=ndarrays
python -m pytest -n auto -rXx ${FLAGS} -m "not (parallel or xdist_incompatible) and not (c or python) ${{ inputs.pytest_mark }}" --ignore=symbolic --ignore=ndarrays
python -m pytest -rXx ${FLAGS} -m "xdist_incompatible and not parallel and not (c or python) ${{ inputs.pytest_mark }}" --ignore=symbolic --ignore=ndarrays
pyccel-clean
python -m pytest ndarrays/ -rXx
python -m pytest ndarrays/ -rXx ${FLAGS}
pyccel-clean
shell: ${{ inputs.shell_cmd }}
working-directory: ./tests
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ All notable changes to this project will be documented in this file.

### Changed

- Updating `stdlib` files if they are modified not just accessed.

### Deprecated

## \[1.8.1\] - 2023-07-07
Expand Down
65 changes: 53 additions & 12 deletions pyccel/codegen/compiling/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ class CompileObj:
program. If no name is provided then the module name deduced from the file
name is used.
"""
compilation_in_progress = FileLock('.lock_acquisition.lock')
__slots__ = ('_file','_folder','_module_name','_module_target','_prog_target',
'_lock','_flags','_includes','_libs','_libdirs','_accelerators',
'_dependencies','_has_target_file')
'_lock_target','_lock_source','_flags','_includes','_libs',
'_libdirs','_accelerators','_dependencies','_has_target_file')
def __init__(self,
file_name,
folder,
Expand Down Expand Up @@ -86,7 +87,8 @@ def __init__(self,
self._prog_target = self._prog_target
self._module_target = self._module_target

self._lock = FileLock(self.module_target+'.lock')
self._lock_target = FileLock(self.module_target+'.lock')
self._lock_source = FileLock(self.source+'.lock')

self._flags = list(flags)
if has_target_file:
Expand All @@ -101,13 +103,26 @@ def __init__(self,

def reset_folder(self, folder):
"""
Change the folder in which the source file is saved (useful for stdlib)
Change the folder in which the source file is saved.

Change the folder in which the source file is saved. Normally the location
of the source file should not change during the execution, however when
working with the stdlib, the `CompileObj` is created with the folder set
to the file's location in the Pyccel install directory. When the file is
used it is copied to the user's folder, at which point the folder of the
`CompileObj` must be updated.

Parameters
----------
folder : str
The new folder where the source file can be found.
"""
if self.has_target_file:
self._includes.remove(self._folder)
self._includes.add(folder)

self._file = os.path.join(folder, os.path.basename(self._file))
self._lock_source = FileLock(self.source+'.lock')
self._folder = folder
self._includes.add(self._folder)

Expand All @@ -118,7 +133,7 @@ def reset_folder(self, folder):
if sys.platform == "win32":
self._prog_target += '.exe'

self._lock = FileLock(self.module_target+'.lock')
self._lock_target = FileLock(self.module_target+'.lock')

@property
def source(self):
Expand Down Expand Up @@ -221,35 +236,61 @@ def add_dependencies(self, *args):
raise TypeError("Dependencies require necessary compile information")
self._dependencies.update({a.module_target:a for a in args})

def __enter__(self):
self.compilation_in_progress.acquire()
self.acquire_lock()

def acquire_lock(self):
"""
Lock the file and its dependencies to prevent race conditions
Lock the file and its dependencies to prevent race conditions.

Acquire the file locks for the file being compiled, all dependencies needed
to compile it and the target file which will be generated.
"""
self._lock_source.acquire()
self.acquire_simple_lock()
for d in self.dependencies:
d.acquire_simple_lock()

def acquire_simple_lock(self):
"""
Lock the file to prevent race conditions but not its dependencies
Lock the file created by this `CompileObj`.

Acquire the file lock for the file created by this `CompileObj` to prevent
race conditions. This function should be called when the created file is a
dependency, it is therefore not necessary for it to recurse into its own
dependencies.
"""
if self.has_target_file:
self._lock.acquire()
self._lock_target.acquire()

def __exit__(self, exc_type, value, traceback):
self.release_lock()
self.compilation_in_progress.release()

def release_lock(self):
"""
Unlock the file and its dependencies
Unlock the file and its dependencies.

Release the file locks for the file being compiled, all dependencies needed
to compile it and the target file which will be generated.
"""
self.release_simple_lock()
for d in self.dependencies:
d.release_simple_lock()
self._lock_source.release()
self.release_simple_lock()

def release_simple_lock(self):
"""
Unlock the file
Unlock the file created by this `CompileObj`.

Release the file lock for the file created by this `CompileObj` to prevent
race conditions. This function should be called when the created file is a
dependency, it is therefore not necessary for it to recurse into its own
dependencies.
"""
if self.has_target_file:
self._lock.release()
self._lock_target.release()

@property
def accelerators(self):
Expand Down