@@ -184,8 +184,6 @@ def run(
184184
185185 self ._stack .prepare_step_run (info = step_run_info )
186186
187- # Initialize the step context singleton
188- StepContext ._clear ()
189187 step_context = StepContext (
190188 pipeline_run = pipeline_run ,
191189 step_run = step_run ,
@@ -196,75 +194,78 @@ def run(
196194 },
197195 )
198196
199- # Parse the inputs for the entrypoint function.
200- function_params = self ._parse_inputs (
201- args = spec .args ,
202- annotations = spec .annotations ,
203- input_artifacts = input_artifacts ,
204- )
197+ with step_context :
198+ function_params = self ._parse_inputs (
199+ args = spec .args ,
200+ annotations = spec .annotations ,
201+ input_artifacts = input_artifacts ,
202+ )
205203
206- # Get all step environment variables. For most orchestrators, the
207- # non-secret environment variables have been set before by the
208- # orchestrator. But for some orchestrators, this is not possible and
209- # we therefore make sure to set them here so they're at least
210- # available for the user code.
211- step_environment = env_utils .get_step_environment (
212- step_config = step_run .config , stack = self ._stack
213- )
214- secret_environment = env_utils .get_step_secret_environment (
215- step_config = step_run .config , stack = self ._stack
216- )
217- step_environment .update (secret_environment )
218-
219- step_failed = False
220- try :
221- if (
222- # TODO: do we need to disable this for dynamic pipelines?
223- pipeline_run .snapshot
224- and self ._stack .orchestrator .run_init_cleanup_at_step_level
225- ):
226- self ._stack .orchestrator .run_init_hook (
227- snapshot = pipeline_run .snapshot
228- )
204+ # Get all step environment variables. For most orchestrators, the
205+ # non-secret environment variables have been set before by the
206+ # orchestrator. But for some orchestrators, this is not possible and
207+ # we therefore make sure to set them here so they're at least
208+ # available for the user code.
209+ step_environment = env_utils .get_step_environment (
210+ step_config = step_run .config , stack = self ._stack
211+ )
212+ secret_environment = env_utils .get_step_secret_environment (
213+ step_config = step_run .config , stack = self ._stack
214+ )
215+ step_environment .update (secret_environment )
229216
230- with env_utils .temporary_environment (step_environment ):
231- return_values = step_instance .call_entrypoint (
232- ** function_params
233- )
234- except BaseException as step_exception : # noqa: E722
235- step_failed = True
217+ step_failed = False
218+ try :
219+ if (
220+ # TODO: do we need to disable this for dynamic pipelines?
221+ pipeline_run .snapshot
222+ and self ._stack .orchestrator .run_init_cleanup_at_step_level
223+ ):
224+ self ._stack .orchestrator .run_init_hook (
225+ snapshot = pipeline_run .snapshot
226+ )
236227
237- exception_info = exception_utils .collect_exception_information (
238- step_exception , step_instance
239- )
228+ with env_utils .temporary_environment (step_environment ):
229+ return_values = step_instance .call_entrypoint (
230+ ** function_params
231+ )
232+ except BaseException as step_exception : # noqa: E722
233+ step_failed = True
240234
241- if ENV_ZENML_STEP_OPERATOR in os .environ :
242- # We're running in a step operator environment, so we can't
243- # depend on the step launcher to publish the exception info
244- Client ().zen_store .update_run_step (
245- step_run_id = step_run_info .step_run_id ,
246- step_run_update = StepRunUpdate (
247- exception_info = exception_info ,
248- ),
235+ exception_info = (
236+ exception_utils .collect_exception_information (
237+ step_exception , step_instance
238+ )
249239 )
250- else :
251- # This will be published by the step launcher
252- step_exception_info .set (exception_info )
253240
254- if not step_run .is_retriable :
255- if (
256- failure_hook_source
257- := self .configuration .failure_hook_source
258- ):
259- logger .info ("Detected failure hook. Running..." )
260- with env_utils .temporary_environment (step_environment ):
261- load_and_run_hook (
262- failure_hook_source ,
263- step_exception = step_exception ,
264- )
265- raise
266- finally :
267- try :
241+ if ENV_ZENML_STEP_OPERATOR in os .environ :
242+ # We're running in a step operator environment, so we can't
243+ # depend on the step launcher to publish the exception info
244+ Client ().zen_store .update_run_step (
245+ step_run_id = step_run_info .step_run_id ,
246+ step_run_update = StepRunUpdate (
247+ exception_info = exception_info ,
248+ ),
249+ )
250+ else :
251+ # This will be published by the step launcher
252+ step_exception_info .set (exception_info )
253+
254+ if not step_run .is_retriable :
255+ if (
256+ failure_hook_source
257+ := self .configuration .failure_hook_source
258+ ):
259+ logger .info ("Detected failure hook. Running..." )
260+ with env_utils .temporary_environment (
261+ step_environment
262+ ):
263+ load_and_run_hook (
264+ failure_hook_source ,
265+ step_exception = step_exception ,
266+ )
267+ raise
268+ finally :
268269 step_run_metadata = self ._stack .get_step_run_metadata (
269270 info = step_run_info ,
270271 )
@@ -338,12 +339,6 @@ def run(
338339 snapshot = pipeline_run .snapshot
339340 )
340341
341- finally :
342- step_context ._cleanup_registry .execute_callbacks (
343- raise_on_exception = False
344- )
345- StepContext ._clear () # Remove the step context singleton
346-
347342 # Update the status and output artifacts of the step run.
348343 output_artifact_ids = {
349344 output_name : [
0 commit comments