Execute business logic on Workflow execution timeout

I have a parent workflow that executes a child workflow that polls a activity that retrieves the status from another service and child workflow sleeps using workflow.await if the status returned from activity is still “InProgress”. We have a workflow execution timeout of X mins.
Is there a way where we can execute a handle or cleanup function to update database etc from workflow ?
I know polling from child workflow is a anti pattern and for now works instead of activity stealing thread with Thread.sleep.

One workaround I can think of is have a global variable with startTime in workflow and each time before poll check the how much time has elapsed using Workflow.currentTime() and if it has elapsed more than timeout secs, call activity to clean up. But I find this pretty hacky eve though its deterministic. Any other better way to handle this ?

Is there an option to catch workflow execution timeout exception in parent workflow and then execute cleanup acitvity ?

Activity can be implemented asynchronously using manual activity completion to avoid holding a thread.

Using a timer or checking elapsed time in a workflow is the recommended way to handle business level timeouts. Why do you think it is “hacky”?

Thanks for the reply! I meant hacky since, I have to now increase the childworkflow execution timeout to say T + X secs where X is the polling interval when activity is called for status check. Now in the loop which i run in my workflow i have to check if Workflow start time - curr time > T then break and execute clean up activity. i might have to increase the execution timeout to larger value to make sure i give enough time for cleanup activity gets chance. Also becoming hard to unit test this.

Workflow timeout, by definition, is used to terminate workflow without giving it any chance to cleanup.

Temporal provides unit testing support that skips time so that you can test it without a problem.