Automatically cancelling a copy activity when no data is being read.

2025-08-07T09:30:26.35+00:00

I have a data source that sometimes causes copy activities to get stuck - no data is being read or copied. ADF just shows the time to first byte as continually increasing until timeout is reached.

Ideally I want to catch this as early as possible, but doing this by setting a lower timeout can be problematic. Example:

  • Activity in progress > 15 minutes and data is being read/copied - Should not be cancelled.
  • Activity in progress > 5 minutes but no data being read/copied - Should be cancelled.

(It is rare that an activity takes more than a few minutes. But it does happen)

Is it in anyway possible to cancel a copy activity in ADF only when no data is being read or copied - using ADF, logic apps or any other Azure application?

I know I can view the time to first byte, rows read, etc. in logic apps. But only after the copy activity has completed - so this is no use.

Azure Data Factory
Azure Data Factory
An Azure service for ingesting, preparing, and transforming data at scale.
0 comments No comments
{count} votes

Accepted answer
  1. Pratyush Vashistha 975 Reputation points Microsoft External Staff Moderator
    2025-08-07T10:43:47.8833333+00:00

    Hello Lockyer, Aaron (SGRE COG B MS SYS BSPM)!

    Thanks for posting your query on Microsoft QnA! and explaining your use case. Thanks Alex Burlachenko for your detailed answer. In addition to Alex Burlachenko answer, I would like to add my findings.

    There isn’t a built-in ADF feature that lets you automatically cancel a copy activity in real-time based on whether data is actually being read or written. The pipeline activity timeout setting is global for the entire activity and won’t distinguish between an active but slow transfer and an idle one with no data movement. Also, the diagnostic metrics and logs for “rows read,” “bytes written,” or “time to first byte” are only available after the activity finishes; there isn’t support for subscribing to these indicators while the copy job is still in progress.

    A few common workarounds are available, but each has limitations:

    • Lower the overall activity timeout: This will forcibly stop long-running jobs, but it doesn’t check if data is being transferred and might cancel jobs that are just slow, not idle.
    • Use external automation with Logic Apps, Azure Functions, or PowerShell to poll ADF activity runs using the Data Factory REST API. However, this approach can only see metrics once the activity is done—not “live” data movement status. There is currently no API field or metric indicating “last data received” or “rows read so far” in real-time.

    There’s no direct way in Azure Data Factory (ADF) to automatically cancel a copy activity in progress based on detecting an idle state (no data read/copied) via native features. The standard activity timeout you can set applies to the entire activity regardless of whether any data movement is occurring—so lowering it risks stopping healthy, slow-running jobs as well as those that are truly stuck. The ADF UI and monitoring tools only expose details such as “rows read” and “time to first byte” after the copy activity completes, not in real time, which means nothing built-in can act on live, mid-job progress to trigger a targeted cancellation.

    You can configure timeouts and monitor pipeline runs after the fact using ADF monitoring and Activity Run outputs (copy activity timeouts documentation), but live progress metrics are not currently surfaced in the API or monitoring interface. Automation with services like Logic Apps or scheduled checks via the Data Factory REST API also only allow you to act on job-level status (Succeeded, Failed, Cancelled, etc.) and cannot see in-flight, per-minute data movement.

    To summarize, while you can set copy activity-level timeouts in ADF, there is currently no supported method to automatically detect and cancel only copy activities that are idle (not moving data) in real time.

    Reference:

    If this control is important to your data flows, consider submitting feedback through Microsoft’s documentation or product feedback channels, as this is a recognized area with opportunity for improvement on the platform.

    Please "Accept as Answer" or do a Upvote if the answer provided is useful, so that you can help others in the community looking for remediation for similar issues.

    Thanks

    Pratyush


2 additional answers

Sort by: Most helpful
  1. Alex Burlachenko 13,330 Reputation points Volunteer Moderator
    2025-08-07T10:28:40.96+00:00

    hi aaron! thanks for posting this tricky question ))

    so u wanna stop copy activities when they're stuck but keep them running if they're actually working... got it! here's how we can tackle this with some azure magic )

    u can use a combination of azure functions and data factory's monitoring api. set up a function that checks the copy activity's progress while it runs. the function can peek at metrics like 'rows read' and 'data read' through the api. if nothing moves for X minutes, boom – it cancels the activity https://docs.microsoft.com/en-us/rest/api/datafactory/pipeline-runs/get

    and here's a little code snippet to get u started

    var response = await client.PipelineRuns.GetWithHttpMessagesAsync( resourceGroupName, factoryName, runId); var metrics = response.Body.RunOutput;

    worth looking into azure monitor alerts too. u can create custom alerts based on activity metrics. if 'rows read' stays at zero for too long, trigger an action ))

    sometimes the issue might be with the data source itself. maybe its throttling connections or just being slow. u could add a pre-copy check that tests the connection speed first.

    when dealing with flaky data sources, always have a plan B. maybe add retry logic with exponential backoff. the azure sdk makes this pretty easy to implement )

    and dont forget – u can use data factory's built-in retry settings. tweak those before going full custom solution. sometimes simple fixes work best %)

    let me know if u hit any snags

    Alex

    and "yes" if you would follow me at Q&A - personaly thx.
    P.S. If my answer help to you, please Accept my answer
    

    https://ctrlaltdel.blog/

    0 comments No comments

  2. Alex Burlachenko 13,330 Reputation points Volunteer Moderator
    2025-08-07T10:46:51.98+00:00

    hi aaron ))

    so u got this tricky situation where ur copy activity just hangs there like a lazy cat, no data moving, just time to first byte creeping up... ugh, frustrating )) good news is, we can fix this with some clever tricks, for azure data factory specifically u can use a pipeline watchdog approach.

    1. add a until loop right after ur copy activity. inside it, place a web activity that checks the copy activity's status using the ADF monitoring API.

    make the web activity call this endpoint https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DataFactory/factories/{factoryName}/pipelineruns/{runId}/queryActivityruns?api-version=2018-06-01. yeah, long url, but it works.

    parse the response to check rowsRead and timeToFirstByte. if no rows read and time keeps climbing, u can bail out early.

    quick json snippet for the web activity body

    { "lastUpdatedAfter": "@{addminutes(utcnow(), -5)}", "lastUpdatedBefore": "@{utcnow()}" }

    full docs here https://docs.microsoft.com/en-us/rest/api/datafactory/activity-runs/query-by-pipeline-run

    ever tried azure monitor alerts? u can set metrics to trigger when data flow stalls. not perfect, but better than nothing.

    logic apps can monitor this, but yeah, only after completion... sad. still, worth keeping in mind for other workflows.

    if u use custom logging, u could track bytes read per minute. then auto-kill if it flatlines. bit hacky, but hey, it works ))

    if u wanna get fancy, u could wrap this whole thing in an azure function. it'd poll the ADF api every minute, then force-cancel if nothing moves. overkill? maybe. cool? absolutely :))

    let me know if u hit snags

    Alex

    and "yes" if you would follow me at Q&A - personaly thx.
    P.S. If my answer help to you, please Accept my answer
    

    https://ctrlaltdel.blog/

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.