Skip to content

Commit 4f0593e

Browse files
committed
fix(workflow): scope _deploy cleanup to what it actually changed
- PREFECT_API_URL was popped even when the gateway shim never ran - httpx.AsyncClient.__init__ leaked if shim setup or the loop raised - replace unawaited/skipped deploy tests with 11 executing tests - workflow.py coverage 26% -> 43% - drop permanently skipped test_deploy_runner
1 parent 0d32bc2 commit 4f0593e

2 files changed

Lines changed: 442 additions & 88 deletions

File tree

‎src/osw/utils/workflow.py‎

Lines changed: 63 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -671,75 +671,78 @@ async def _deploy(param: DeployParam):
671671

672672
_original_api_url = None
673673
_original_httpx_init = None
674+
_api_url_overridden = False
674675
gateway_url = param.public_url or environ.get("PREFECT_API_URL", "")
675-
if _is_apigateway_url(gateway_url) and param.osw is not None:
676-
_original_api_url = environ.get("PREFECT_API_URL")
677-
environ["PREFECT_API_URL"] = gateway_url
678-
_mw_site = param.osw.site.mw_site
679-
680-
def _relogin():
681-
cred = param.osw.site._cred_mngr.get_credential(param.osw.site._iri)
682-
_mw_site.login(username=cred.username, password=cred.password)
683-
684-
_gw_transport = ApiGatewayTransport(
685-
gateway_url=gateway_url,
686-
mw_site=_mw_site,
687-
relogin_cb=_relogin,
688-
)
689-
# Patch httpx.AsyncClient to auto-inject our transport when
690-
# the base_url is an ApiGateway URL. One patch covers ALL
691-
# Prefect client instances regardless of how they're created.
692-
_original_httpx_init = httpx.AsyncClient.__init__
693-
694-
def _patched_httpx_init(self, *args, **kwargs):
695-
base = str(kwargs.get("base_url", ""))
696-
if _is_apigateway_url(base) and "transport" not in kwargs:
697-
kwargs["transport"] = _gw_transport
698-
_original_httpx_init(self, *args, **kwargs)
699-
700-
httpx.AsyncClient.__init__ = _patched_httpx_init
701-
702-
# Auto-install .pth hook so Prefect subprocesses also get patched
703-
_pth_target = os.path.join(_get_site_packages(), _PTH_FILENAME)
704-
if not os.path.exists(_pth_target):
705-
install_gateway_hook()
706-
707-
deployments = []
708-
709-
for deploy_config in param.deployments:
710-
flow: Flow = deploy_config.flow
711-
# Set deployment name if not provided
712-
if deploy_config.name is None or deploy_config.name == "":
713-
deploy_config.name = flow.name + "-deployment"
714-
715-
# Match valid args of flow.to_deployment and deploy_config
716-
kwargs = match_func_model_args(func=flow.to_deployment, model=deploy_config)
717-
# Set config via matching flow.to_deployment arguments
718-
config = await flow.to_deployment(**kwargs)
719-
await config.apply() # returns the deployment_uuid
720-
721-
deployments.append(config)
722-
723-
# Register flow on OSW if an instance is provided
724-
if param.osw is not None:
725-
await register_flow(
726-
osw_instance=param.osw,
727-
flow=flow,
728-
namespace_uuid=param.namespace_uuid,
729-
public_url=param.public_url,
676+
try:
677+
if _is_apigateway_url(gateway_url) and param.osw is not None:
678+
_original_api_url = environ.get("PREFECT_API_URL")
679+
environ["PREFECT_API_URL"] = gateway_url
680+
_api_url_overridden = True
681+
_mw_site = param.osw.site.mw_site
682+
683+
def _relogin():
684+
cred = param.osw.site._cred_mngr.get_credential(param.osw.site._iri)
685+
_mw_site.login(username=cred.username, password=cred.password)
686+
687+
_gw_transport = ApiGatewayTransport(
688+
gateway_url=gateway_url,
689+
mw_site=_mw_site,
690+
relogin_cb=_relogin,
730691
)
692+
# Patch httpx.AsyncClient to auto-inject our transport when
693+
# the base_url is an ApiGateway URL. One patch covers ALL
694+
# Prefect client instances regardless of how they're created.
695+
_original_httpx_init = httpx.AsyncClient.__init__
696+
697+
def _patched_httpx_init(self, *args, **kwargs):
698+
base = str(kwargs.get("base_url", ""))
699+
if _is_apigateway_url(base) and "transport" not in kwargs:
700+
kwargs["transport"] = _gw_transport
701+
_original_httpx_init(self, *args, **kwargs)
702+
703+
httpx.AsyncClient.__init__ = _patched_httpx_init
704+
705+
# Auto-install .pth hook so Prefect subprocesses also get patched
706+
_pth_target = os.path.join(_get_site_packages(), _PTH_FILENAME)
707+
if not os.path.exists(_pth_target):
708+
install_gateway_hook()
709+
710+
deployments = []
711+
712+
for deploy_config in param.deployments:
713+
flow: Flow = deploy_config.flow
714+
# Set deployment name if not provided
715+
if deploy_config.name is None or deploy_config.name == "":
716+
deploy_config.name = flow.name + "-deployment"
717+
718+
# Match valid args of flow.to_deployment and deploy_config
719+
kwargs = match_func_model_args(func=flow.to_deployment, model=deploy_config)
720+
# Set config via matching flow.to_deployment arguments
721+
config = await flow.to_deployment(**kwargs)
722+
await config.apply() # returns the deployment_uuid
723+
724+
deployments.append(config)
725+
726+
# Register flow on OSW if an instance is provided
727+
if param.osw is not None:
728+
await register_flow(
729+
osw_instance=param.osw,
730+
flow=flow,
731+
namespace_uuid=param.namespace_uuid,
732+
public_url=param.public_url,
733+
)
731734

732-
try:
733735
await serve(*deployments)
734736
finally:
735737
# Restore patched httpx.AsyncClient.__init__
736738
if _original_httpx_init is not None:
737739
httpx.AsyncClient.__init__ = _original_httpx_init
738740
# Restore original PREFECT_API_URL if we overrode it
739-
if _original_api_url is not None:
740-
environ["PREFECT_API_URL"] = _original_api_url
741-
elif _is_apigateway_url(gateway_url):
742-
environ.pop("PREFECT_API_URL", None)
741+
if _api_url_overridden:
742+
if _original_api_url is not None:
743+
environ["PREFECT_API_URL"] = _original_api_url
744+
else:
745+
environ.pop("PREFECT_API_URL", None)
743746

744747

745748
def deploy(param: DeployParam):

0 commit comments

Comments
 (0)