Skip to content

apify run hangs for 60s after Actor exit when APIFY_TOKEN is set #1262

Description

@DaveHanns

Summary

When APIFY_TOKEN is set in the environment, apify run does not return control to the shell after the Actor calls Actor.exit(). It hangs until the caller times out (observed ~60s in a CI wrapper). Unsetting APIFY_TOKEN before the same command makes the run return in under a second, so the token being forwarded to the child is what triggers the stall.

Reproducer

Minimal Node.js Actor that exits cleanly:

// src/main.js
import { Actor } from 'apify';
await Actor.init();
console.log('hello');
await Actor.exit();

Then:

# Hangs until the outer caller kills it (~60s in our runner):
APIFY_TOKEN=<real-token> apify run

# Returns in <1s:
env -u APIFY_TOKEN apify run

Observed behavior with the token set — the Actor's own logs finish, but the CLI process does not release the shell:

Exit code 143
Command timed out after 1m 0s

Suspected cause

src/commands/run.ts forwards the token to the child via localEnvVars[APIFY_ENV_VARS.TOKEN] = token. The Actor SDK in the child then treats the run as a real platform run and attempts to POST the terminal status to the Apify API. In an offline / rate-limited / firewalled environment that POST never completes, and the child (and therefore apify run) blocks on it.

I'm not 100% sure whether the correct fix belongs in the CLI or the SDK, hence filing an issue rather than a PR. Two plausible directions:

  1. CLI side (src/commands/run.ts): don't forward APIFY_TOKEN to the child by default during apify run. Gate the current behavior behind an opt-in flag (e.g. --report-to-cloud / --forward-token). This keeps local runs offline-safe. Apify Proxy usage during local runs would need to fall back to the proxy password already available via getLocalUserInfo().
  2. SDK side: bound the terminal-status POST with a short hard timeout and no retries when the run was launched locally (no ACTOR_ID / ACTOR_RUN_ID env), so a stalled call can never block Actor.exit() propagation.

Either would prevent the hang. The user-visible symptom in the CLI is severe (looks like the CLI itself is broken), so I lean toward option 1 as the primary fix.

Impact

Any environment where the network path to the Apify API is slow or blocked but APIFY_TOKEN is present in the shell (developer laptops on flaky Wi-Fi, restricted CI runners, sandboxed evaluation harnesses) sees every apify run hang for the full outer timeout. The workaround (env -u APIFY_TOKEN apify run) is not discoverable.

Environment

  • apify-cli: latest at time of report
  • Node.js: 20.x
  • OS: macOS 14 / Linux CI runner

Surfaced during an evaluation of Apify surfaces for agent-driven Actor development.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions