fix: release CAD COM references before apartment shutdown
This commit is contained in:
1 parent
f3d2570e48
commit
fff39e0645
5 files changed
+143
-4
No files matched your search
@@ -1,10 +1,12 @@
|
||||
import io
|
||||
import json
|
||||
import os
|
||||
import weakref
|
||||
from pathlib import Path
|
||||
from threading import Event
|
||||
from threading import Timer
|
||||
from types import SimpleNamespace
|
||||
from traceback import extract_tb
|
||||
|
||||
import pytest
|
||||
|
||||
@@ -12,6 +14,63 @@ from dx_productivity.trial_task_center import isolated_cad as runtime
|
||||
from dx_productivity.trial_task_center.worker_monitor import WorkerMonitorError
|
||||
|
||||
|
||||
@pytest.mark.skipif(os.name != 'nt', reason='Windows COM helper boundary')
|
||||
@pytest.mark.parametrize('failure', [None, 'open_project', 'quit', 'guid', 'cause', 'context'])
|
||||
def test_quit_helper_releases_application_before_com_uninitialization(monkeypatch, tmp_path, failure):
|
||||
import pythoncom
|
||||
import win32com.client
|
||||
from dx_productivity.trial_task_center import cad_identity
|
||||
references = []
|
||||
alive_at_uninitialize = []
|
||||
def nested_failure(app):
|
||||
raise ValueError('nested quit error')
|
||||
class Application:
|
||||
def Guid(self):
|
||||
return '{foreign}' if failure == 'guid' else '{owned}'
|
||||
def Quit(self):
|
||||
if failure == 'quit':
|
||||
raise RuntimeError('quit refused')
|
||||
if failure in ('cause', 'context'):
|
||||
try:
|
||||
nested_failure(self)
|
||||
except ValueError as error:
|
||||
if failure == 'cause':
|
||||
raise RuntimeError('quit refused') from error
|
||||
raise RuntimeError('quit refused')
|
||||
def attach(_name):
|
||||
app = Application()
|
||||
references.append(weakref.ref(app))
|
||||
return app
|
||||
monkeypatch.setattr(pythoncom, 'CoInitialize', lambda: None)
|
||||
monkeypatch.setattr(pythoncom, 'CoUninitialize',
|
||||
lambda: alive_at_uninitialize.append(references[0]() is not None))
|
||||
monkeypatch.setattr(win32com.client, 'GetActiveObject', attach)
|
||||
monkeypatch.setattr(runtime, 'current_desktop_name', lambda: 'dx-test')
|
||||
monkeypatch.setattr(cad_identity, 'current_desktop_name', lambda: 'dx-test')
|
||||
monkeypatch.setattr(runtime, 'project_file_path', lambda app: 'open.prj' if failure == 'open_project' else '')
|
||||
for key, value in dict(DXPART_DESKTOP='dx-test', DXPART_CAD_PID='123',
|
||||
DXPART_CAD_GUID='{owned}', DXPART_SESSION_TOKEN='a' * 32).items():
|
||||
monkeypatch.setenv(key, value)
|
||||
if failure == 'open_project':
|
||||
with pytest.raises(ValueError, match='still has a project open'):
|
||||
runtime._native_action('quit', tmp_path / 'evidence.json', 123)
|
||||
elif failure == 'guid':
|
||||
with pytest.raises(ValueError, match='GUID does not match'):
|
||||
runtime._native_action('quit', tmp_path / 'evidence.json', 123)
|
||||
elif failure:
|
||||
with pytest.raises(RuntimeError, match='quit refused') as caught:
|
||||
runtime._native_action('quit', tmp_path / 'evidence.json', 123)
|
||||
assert 'Quit' in [frame.name for frame in extract_tb(caught.value.__traceback__)]
|
||||
if failure in ('cause', 'context'):
|
||||
assert str(caught.value.__context__) == 'nested quit error'
|
||||
assert 'nested_failure' in [frame.name for frame in extract_tb(caught.value.__context__.__traceback__)]
|
||||
if failure == 'cause':
|
||||
assert str(caught.value.__cause__) == 'nested quit error'
|
||||
else:
|
||||
assert runtime._native_action('quit', tmp_path / 'evidence.json', 123) == 0
|
||||
assert alive_at_uninitialize == [False]
|
||||
|
||||
|
||||
@pytest.mark.skipif(os.name != 'nt', reason='Windows junction path handling')
|
||||
def test_private_evidence_uses_canonical_root_behind_a_legacy_alias(tmp_path):
|
||||
from dx_productivity.trial_task_center.legacy_paths import _AliasPool
|
||||
|
||||
@@ -11,6 +11,27 @@ from dx_productivity.trial_task_center.isolated_desktop import PrivateDesktop, b
|
||||
from dx_productivity.trial_task_center.isolated_desktop import _Win32Api, _Win32Process
|
||||
|
||||
|
||||
@pytest.mark.skipif(os.name != 'nt', reason='native Windows deferred desktop release')
|
||||
def test_release_waits_for_a_temporary_external_reference_without_stopping_its_owner():
|
||||
desktop = PrivateDesktop('dx-test-' + uuid4().hex, enabled=True)
|
||||
desktop.__enter__()
|
||||
retained = subprocess.Popen((sys.executable, '-u', '-c',
|
||||
'import win32service,sys,time; h=win32service.OpenDesktop(sys.argv[1],0,False,1); '
|
||||
'print("ready",flush=True); time.sleep(3); h.CloseDesktop(); '
|
||||
'print("released",flush=True); time.sleep(30)', desktop.name), stdout=subprocess.PIPE)
|
||||
try:
|
||||
assert retained.stdout.readline() == b'ready\r\n'
|
||||
desktop.close()
|
||||
assert desktop.cleanup_record['desktop_confirmation']['released']
|
||||
assert retained.stdout.readline() == b'released\r\n'
|
||||
assert retained.poll() is None
|
||||
finally:
|
||||
retained.terminate()
|
||||
retained.wait(timeout=5)
|
||||
retained.stdout.close()
|
||||
desktop.close()
|
||||
|
||||
|
||||
@pytest.mark.skipif(os.name != 'nt', reason='native Windows desktop lifetime')
|
||||
def test_release_does_not_report_success_while_another_handle_keeps_desktop_alive():
|
||||
desktop = PrivateDesktop('dx-test-' + uuid4().hex, enabled=True)
|
||||
|
||||
Reference in new issue
Block a user