Skip to content

Commit 7625f49

Browse files
committed
Fix a process-wide global singleton
1 parent 6e23bc1 commit 7625f49

5 files changed

Lines changed: 49 additions & 8 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"comment": "Modify this file in a trivial way to cause this test suite to run",
3+
"revision": 1
4+
}

sdks/python/apache_beam/runners/portability/prism_runner_test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
import apache_beam as beam
3939
from apache_beam.options.pipeline_options import DebugOptions
40+
from apache_beam.options.pipeline_options import PipelineOptions
4041
from apache_beam.options.pipeline_options import PortableOptions
4142
from apache_beam.options.pipeline_options import StandardOptions
4243
from apache_beam.options.pipeline_options import TypeOptions
@@ -49,6 +50,7 @@
4950
from apache_beam.transforms import trigger
5051
from apache_beam.transforms import window
5152
from apache_beam.utils import shared
53+
from apache_beam.utils import subprocess_server
5254

5355
# Run as
5456
#

sdks/python/apache_beam/transforms/external_test.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,13 @@ def test_implicit_builder_with_constructor_method(self):
799799

800800
class JavaJarExpansionServiceTest(unittest.TestCase):
801801
def setUp(self):
802-
SubprocessServer._cache._live_owners = set()
802+
SubprocessServer._cache._purge_buffer.append({
803+
'owner': 'reset', 'pid': os.getpid(), 'live_owners': []
804+
})
805+
patcher = mock.patch.object(
806+
SubprocessServer._cache, '_live_owners', new=set())
807+
patcher.start()
808+
self.addCleanup(patcher.stop)
803809

804810
def test_classpath(self):
805811
with tempfile.TemporaryDirectory() as temp_dir:

sdks/python/apache_beam/utils/subprocess_server.py

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,25 +76,47 @@ def __init__(self, constructor, destructor):
7676
self._cache = {}
7777
self._lock = threading.Lock()
7878
self._counter = 0
79+
self._purge_buffer = []
80+
self._owner2_buffer = []
7981

8082
def _next_id(self):
81-
with self._lock:
82-
self._counter += 1
83-
return self._counter
83+
# Caller must hold self._lock.
84+
self._counter += 1
85+
return self._counter
8486

8587
def register(self):
86-
owner = self._next_id()
87-
self._live_owners.add(owner)
88+
with self._lock:
89+
owner = self._next_id()
90+
self._live_owners.add(owner)
91+
if owner == 2:
92+
self._owner2_buffer.append({
93+
'action': 'register',
94+
'pid': os.getpid(),
95+
'live_owners': list(self._live_owners)
96+
})
8897
return owner
8998

9099
def purge(self, owner):
91100
to_delete = []
92101
with self._lock:
102+
self._purge_buffer.append({
103+
'owner': owner,
104+
'pid': os.getpid(),
105+
'live_owners': ("%s" % self._live_owners),
106+
})
107+
if owner == 2:
108+
self._owner2_buffer.append({
109+
'action': 'purge',
110+
'pid': os.getpid(),
111+
'live_owners': list(self._live_owners)
112+
})
93113
if owner not in self._live_owners:
94114
_LOGGER.warning(
95115
"Subprocess owner %s already purged. If this occurs during atexit "
96-
"shutdown, the subprocess was already cleaned up earlier.",
97-
owner)
116+
"shutdown, the subprocess was already cleaned up earlier. Purge buffer: %s. Owner 2 buffer: %s",
117+
owner,
118+
self._purge_buffer,
119+
self._owner2_buffer)
98120
return
99121
self._live_owners.remove(owner)
100122
for key, entry in list(self._cache.items()):
@@ -103,6 +125,11 @@ def purge(self, owner):
103125
if not entry.owners:
104126
to_delete.append(entry.obj)
105127
del self._cache[key]
128+
if to_delete:
129+
_LOGGER.warning(
130+
"Purging subprocess. Purge buffer: %s. Owner 2 buffer: %s",
131+
self._purge_buffer,
132+
self._owner2_buffer)
106133
# Actually call the destructors outside of the lock.
107134
for value in to_delete:
108135
self._destructor(value)

sdks/python/pytest.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
[pytest]
1919
junit_family = xunit2
20+
log_cli = true
21+
log_cli_level = WARNING
2022
filterwarnings =
2123
ignore:Deprecated call to `pkg_resources.declare_namespace\('.*'\):DeprecationWarning
2224
ignore::DeprecationWarning:google.rpc

0 commit comments

Comments
 (0)