Skip to content

Commit

Permalink
Fix bug on macOS from pickling a closure
Browse files Browse the repository at this point in the history
Resolves: #1314
  • Loading branch information
dgerlanc committed Nov 27, 2022
1 parent c34be65 commit 78339bb
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions tests/link/c/test_cmodule.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import logging
import multiprocessing
import os
import sys
import tempfile
from unittest.mock import patch

Expand Down Expand Up @@ -221,20 +222,28 @@ def test_linking_patch(listdir_mock, platform):
]


@config.change_flags(on_opt_error="raise", on_shape_error="raise")
def _vec_times_constant(k):
# Some of the caching issues arise during constant folding within the
# optimization passes, so we need these config changes to prevent the
# exceptions from being caught
a = at.vector()
f = aesara.function([a], k * a)
return f(np.array([1], dtype=config.floatX))


def test_cache_race_condition():

with tempfile.TemporaryDirectory() as dir_name:

@config.change_flags(on_opt_error="raise", on_shape_error="raise")
def f_build(factor):
# Some of the caching issues arise during constant folding within the
# optimization passes, so we need these config changes to prevent the
# exceptions from being caught
a = at.vector()
f = aesara.function([a], factor * a)
return f(np.array([1], dtype=config.floatX))
# On macOS, spawn is the default starting with Python 3.8
# because fork is unsafe per bpo-33725
# See https://bugs.python.org/issue?@action=redirect&bpo=33725
if sys.platform == "darwin" and sys.version_info < (3, 8):
ctx = multiprocessing.get_context("spawn")
else:
ctx = multiprocessing.get_context()

ctx = multiprocessing.get_context()
compiledir_prop = aesara.config._config_var_dict["compiledir"]

# The module cache must (initially) be `None` for all processes so that
Expand All @@ -245,14 +254,13 @@ def f_build(factor):

assert aesara.config.compiledir == dir_name

num_procs = 30
rng = np.random.default_rng(209)
num_procs = 10
factors = [0.1, 0.257]

for i in range(10):
# A random, constant input to prevent caching between runs
factor = rng.random()
# Use constant inputs to prevent caching between runs
for factor in factors:
procs = [
ctx.Process(target=f_build, args=(factor,))
ctx.Process(target=_vec_times_constant, args=(factor,))
for i in range(num_procs)
]
for proc in procs:
Expand Down

0 comments on commit 78339bb

Please sign in to comment.