> ## Documentation Index
> Fetch the complete documentation index at: https://docs.tensorlake.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# How clocks work in sandboxes

> Understand how monotonic, boottime, and wall clocks behave across suspend, resume, and memory snapshots, and control host wall-clock sync.

Tensorlake sandboxes freeze `CLOCK_MONOTONIC` and `CLOCK_BOOTTIME` while suspended so timers and watchdogs keep their remaining budgets across pause and resume. By default, VM sandboxes resynchronize `CLOCK_REALTIME` (wall time) to host Coordinated Universal Time (UTC) on boot, every 10s, and after resume. You can disable host wall-clock sync at runtime from inside your sandbox or through the sandbox HTTP API when your workload needs wall time frozen across suspension or wants a guest time daemon to own the clock.

## How clocks behave across suspend and resume

Suspend, resume, and [memory snapshots](https://docs.tensorlake.ai/sandboxes/snapshots.md) share one clock contract on VM sandboxes. When a sandbox suspends, the hypervisor captures guest timer state and excludes time spent draining devices, exporting checkpoints, or waiting paused from monotonic clocks.

| Clock | Behavior across suspension and memory snapshots |
| - | - |
| `CLOCK_MONOTONIC` | Excludes external suspension. Remaining timer and watchdog budgets resume where they paused. |
| `CLOCK_BOOTTIME` and `/proc/uptime` | Also excludes external suspension. Preserves any existing offset from `CLOCK_MONOTONIC`. |
| `CLOCK_REALTIME` | Re-anchored asynchronously to host UTC by the in-guest daemon on boot, every 10s, and after resume when drift reaches at least 100 ms, unless you disable wall-clock sync. |

Unlike native Linux sleep, where `CLOCK_BOOTTIME` advances during sleep, external sandbox suspension excludes paused time from both `CLOCK_MONOTONIC` and `CLOCK_BOOTTIME`. Ordinary CPU idle time and host scheduling delays still count as awake time.

Each restore from a memory snapshot continues its own independent captured timeline. Restoring from a filesystem-only snapshot starts a fresh kernel with reset boot clocks and wall-clock sync enabled.

## Default wall-clock sync

On VM sandboxes, the in-guest daemon reads host UTC from the Kernel-based Virtual Machine (KVM) virtual Precision Time Protocol (PTP) device (`/dev/ptp0`, verified against `/sys/class/ptp/ptp0/clock_name`). An initial boot sync finishes before your sandbox entrypoint runs, so boot-time TLS validation and timestamps start on current UTC even if your entrypoint turns sync off right away.

While wall-clock sync stays enabled, the daemon steps `CLOCK_REALTIME` whenever the absolute offset from host UTC reaches at least 100 ms:

* **On boot**: runs one initial check before the daemon releases your entrypoint
* **Every 10s**: corrects steady-state clock drift or host Network Time Protocol (NTP) adjustments
* **On resume**: wakes immediately from `/dev/vmclock0` after pause and resume or memory-snapshot restore, without blocking sandbox readiness

The daemon doesn’t inspect `/proc` for guest time services such as `chronyd` or `systemd-timesyncd`. If your image runs its own NTP daemon or relies on a custom wall-clock timeline, disable the built-in syncer explicitly so the two don’t compete.

## Control wall-clock sync at runtime

You can read or toggle wall-clock sync from inside the sandbox over a root-only Unix domain socket, or from outside the sandbox through the authenticated sandbox HTTP API. Both endpoints drive the same in-memory controller inside the guest daemon.

> [!NOTE]
>
> Disable requests wait for any in-flight `clock_settime` call to finish before returning `200 OK`, so no correction races with your workload after the call succeeds. Re-enabling wakes the syncer immediately to re-anchor `CLOCK_REALTIME` to host UTC.

> [tab] Inside the sandbox (Unix socket)
>
> The guest daemon binds `/run/tensorlake/guest.sock` before releasing your entrypoint. Only `root` (mode `0600` plus kernel `SO_PEERCRED` verification for UID `0`) can connect, and the socket exposes only `/v1/clock-sync`. Host-only daemon ports (`9500`, `9501`, and `9502`) stay unreachable from inside the sandbox.
>
> Disable wall-clock sync in your entrypoint with `curl` before starting a rollout or custom timeline:
>
> ```bash theme={null}
> curl -fsS --unix-socket /run/tensorlake/guest.sock \
>   -X PUT \
>   -H "Content-Type: application/json" \
>   -d '{"enabled": false}' \
>   http://localhost/v1/clock-sync
> ```
>
> This sends a `PUT` request over `/run/tensorlake/guest.sock` and returns the updated JSON status once any in-flight clock step has finished.
>
> Check the current state at any time with `GET`:
>
> ```bash theme={null}
> curl -fsS --unix-socket /run/tensorlake/guest.sock \
>   http://localhost/v1/clock-sync
> ```
>
> Re-enable host UTC syncing later by sending `{"enabled": true}`:
>
> ```bash theme={null}
> curl -fsS --unix-socket /run/tensorlake/guest.sock \
>   -X PUT \
>   -H "Content-Type: application/json" \
>   -d '{"enabled": true}' \
>   http://localhost/v1/clock-sync
> ```
>
> If your base image includes Python instead of `curl`, send the same HTTP request over `socket.AF_UNIX`:
>
> ```python theme={null}
> import json
> import socket
>
> body = json.dumps({"enabled": False}).encode()
> request = (
>     "PUT /v1/clock-sync HTTP/1.1\r\n"
>     "Host: localhost\r\n"
>     "Content-Type: application/json\r\n"
>     f"Content-Length: {len(body)}\r\n"
>     "Connection: close\r\n\r\n"
> ).encode() + body
>
> with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as sock:
>     sock.connect("/run/tensorlake/guest.sock")
>     sock.sendall(request)
>     response = b"".join(iter(lambda: sock.recv(4096), b""))
>
> headers, payload = response.split(b"\r\n\r\n", 1)
> assert headers.startswith(b"HTTP/1.1 200 "), headers
> print(json.loads(payload))
> ```
>
> This opens `/run/tensorlake/guest.sock`, sends a single `PUT /v1/clock-sync` request with `Connection: close`, and prints the parsed JSON status.

> [tab] Outside the sandbox (HTTP API)
>
> Set `SANDBOX_URL` to the `sandbox_url` returned by [Get Sandbox](https://docs.tensorlake.ai/api-reference/v2/sandboxes/get.md) and authenticate with your Tensorlake API key.
>
> Query current clock-sync status with `GET /api/v1/clock-sync`:
>
> ```bash theme={null}
> curl -fsS "${SANDBOX_URL%/}/api/v1/clock-sync" \
>   -H "Authorization: Bearer $TENSORLAKE_API_KEY"
> ```
>
> Disable wall-clock sync before suspending or stepping `CLOCK_REALTIME`:
>
> ```bash theme={null}
> curl -fsS -X PUT "${SANDBOX_URL%/}/api/v1/clock-sync" \
>   -H "Authorization: Bearer $TENSORLAKE_API_KEY" \
>   -H "Content-Type: application/json" \
>   -d '{"enabled": false}'
> ```
>
> This updates the same controller as `/run/tensorlake/guest.sock` and returns `200 OK` with `{"enabled": false, "configurable": true}` once active steps complete. Send `{"enabled": true}` to turn sync back on and re-anchor to host UTC.

## Persistence across lifecycle events

The toggle lives in the guest daemon’s memory rather than on disk, so its behavior matches whether the guest process stays alive across a [sandbox lifecycle](https://docs.tensorlake.ai/sandboxes/lifecycle.md) transition.

| Lifecycle event | Clock-sync state after event |
| - | - |
| Resident suspend and resume | Preserves your `enabled` setting |
| Full-memory snapshot capture and restore | Preserves your `enabled` setting |
| Live migration | Preserves your `enabled` setting |
| Fresh boot or filesystem-only snapshot restore | Resets to `enabled: true` (runs boot UTC sync before your entrypoint runs) |

If your workload restores from a filesystem-only snapshot or cold boots a new sandbox, disable clock sync again in your entrypoint or driver before starting time-sensitive work.

## Runtime support and responses

Both `/v1/clock-sync` and `/api/v1/clock-sync` return `Cache-Control: no-store` and a JSON status object. Check `configurable` before assuming you can turn sync off on a given runtime.

| Runtime or condition | `GET` response | `PUT` response |
| - | - | - |
| VM sandbox with `/dev/ptp0` (`KVM virtual PTP`) and `CAP_SYS_TIME` | `200 OK`: `{"enabled": true, "configurable": true}` | `200 OK` with updated status |
| [GPU sandbox](https://docs.tensorlake.ai/sandboxes/gpu.md) (gVisor runtime) | `200 OK`: `{"enabled": true, "configurable": false, "reason": "..."}` | `501 Not Implemented` (`CLOCK_SYNC_UNSUPPORTED`) |
| VM sandbox without `/dev/ptp0` (`KVM virtual PTP`) or `CAP_SYS_TIME` | `200 OK`: `{"enabled": false, "configurable": false, "reason": "..."}` | `501 Not Implemented` (`CLOCK_SYNC_UNSUPPORTED`) |

Under the gVisor runtime used by GPU sandboxes, the Sentry serves host wall time directly and can’t stop tracking the host clock, so `PUT` fails closed with `501 Not Implemented`.

Requests to `/v1/clock-sync` and `/api/v1/clock-sync` also enforce strict input bounds:

* **Unknown JSON fields**: rejected with `422 Unprocessable Entity`
* **Malformed JSON**: rejected with `400 Bad Request`
* **Request bodies over 4 KB**: rejected with `413 Payload Too Large`
* **Stalled Unix socket connections**: closed after 15s (or immediately on daemon shutdown)

## See also

> [card] [Lifecycle](https://docs.tensorlake.ai/sandboxes/lifecycle.md)
>
> Understand sandbox states, idle timeouts, and suspend and resume.

> [card] [Snapshot and Clone](https://docs.tensorlake.ai/sandboxes/snapshots.md)
>
> Compare memory snapshots and filesystem-only snapshots.

> [card] [Get Clock Sync Status](https://docs.tensorlake.ai/api-reference/v2/runtime/clock-sync-get.md)
>
> HTTP reference for `GET /api/v1/clock-sync`.

> [card] [Set Clock Sync](https://docs.tensorlake.ai/api-reference/v2/runtime/clock-sync-set.md)
>
> HTTP reference for `PUT /api/v1/clock-sync`.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.