Repository navigation
Description
Summary
install-gitlab-webhooks selects its work rows without claiming them. Two runs at the same time process the same rows and can install the same webhook twice. Only the CronJob's concurrencyPolicy: Forbid prevents that today, so the job isn't safe to run concurrently (for example, a manual run during a scheduled one, more than one replica, or a stalled run whose work is reclaimed while its process is still alive).
Split out of #554, which now covers only enrich-user-interaction (fixed by #586).
Details
install-gitlab-webhooks runs every minute when enabled:
GitlabWebhookStore.filter_rowsselects up to 100 rows withwebhook_exists = false, ordered bylast_synced, with no row lock.- Two concurrent runs get the same rows and can both call
create_new_webhookfor the same resource. - The existence check in
check_if_webhook_already_exists_on_resourceruns before the create, so it doesn't close the race between check and create. It matches on URL only, so it can't tell our hooks apart. - GitLab doesn't return a hook's secret token, so a hook with the right URL isn't necessarily one that authenticates.
Impact
- With
concurrencyPolicy: Forbid: none, as long as the job only ever runs from its CronJob. - Whenever two runs overlap: duplicate GitLab webhooks can be installed on the same project or group. Only the hook whose uuid and secret match the row authenticates; the others get 403 on every delivery and stay until someone removes them.
- When the authenticating hook changes (for example, while a duplicate is cleaned up): GitLab queues deliveries per hook and sends them later, so one event can reach us through two hooks that each authenticate at the moment they're sent. The handler's current dedupe is a 60-second Redis key on
object_attributes.id, which doesn't cover that. It also collides across distinct events on the same issue.
Proposed fix
A claim alone doesn't prevent every duplicate. A run whose claim has expired and been taken over can still call GitLab, because the create happens outside our database. The fix has five parts (plan rev 19, section 4.6):
- Claim rows per run. Add
claim_run_idandclaimed_atto the GitLab webhook rows. Claim withSELECT ... FOR UPDATE SKIP LOCKED, stamping the run's ID. Every later write is conditional onWHERE claim_run_id = :run_id, so a run that lost its claim writes nothing (counted asstale_write_rejected). A claim older than the lease may be reclaimed. - Give every hook an identity. Before each create, record an intent (
gitlab_hook_intent: uuid, row, run, stateopen) in a committed transaction. Then create the hook with URLGITLAB_WEBHOOK_URL?oh_hook=<uuid>. The handler is unchanged: it still authenticates by theX-OpenHands-Webhook-IDheader and token. Every change to a row's uuid and secret is a conditional write that names the uuid it replaces and an open intent. A run never reuses an intent, so at most one hook ever carries the row's current identity. - Repair by a rule that stays correct when it runs late. A visit lists the resource's hooks, then reads the intents in a transaction started after the listing. It deletes only hooks whose intent can never become current: abandoned, replaced, or missing. An open intent within its lease is never abandoned, so a live install keeps its hook whatever the visit read earlier. Repair runs as a fast path over recently completed rows plus a daily full sweep.
- Convert legacy hooks rather than adopting them. A hook with the plain URL predates the identity scheme, and its credentials are unknown. Under a row lock, the visit edits the legacy hook with the lowest id to a fresh identity and secret. It deletes the other legacy hooks, then commits the row only if its uuid is unchanged. If the row has no credentials, its legacy hooks can't authenticate, so the visit deletes them and lets the install recreate one.
- Deduplicate deliveries durably. Replace the Redis key with a
gitlab_deliverytable keyed on a sha256 of the raw request body.INSERT ... ON CONFLICT DO NOTHINGruns before any processing, and entries are kept for 7 days.X-Gitlab-Event-UUIDandIdempotency-Keycan't be the key, because GitLab doesn't keep them identical across hooks.
Ownership is already unique: UNIQUE (project_id) and UNIQUE (group_id) (migration 027), plus a per-environment GITLAB_WEBHOOK_URL, mean each resource has one owning row.
Acceptance criteria
- Two overlapping runs claim different rows, and a resource ends up with exactly one hook.
- A run whose claim was taken over writes no row state; its hook gets 403 and is deleted at the next visit.
- A visit reads a row, then a host claims it, creates a hook and commits, then the visit lists and deletes: the new hook survives.
- A visit stalls between deciding and deleting while a host commits a new hook: the new hook survives.
- A create that times out after GitLab made the hook is retried under a new intent, and the first hook is deleted.
- One legacy hook with the right URL and mismatched credentials is converted, and a test delivery through it is accepted.
- Two stale conversions of one row: one commit succeeds, and the other's hook gets 403 and is deleted.
- Before any cleanup, the same event delivered through two authenticating hooks (in both orders and concurrently) starts one conversation.
- Verified on GitLab.com and the oldest supported self-managed version:
- the hooks API returns the
?oh_hook=URL unchanged; PUT .../hooks/:idsets the URL, token and custom headers for project and group hooks;- two hooks on one resource receive byte-identical bodies for one event.
- the hooks API returns the
Guarantees and accepted limits
- No GitLab event is processed twice, however many hooks authenticate.
- The deletion rule never deletes a hook that carries, or can come to carry, the current identity.
- A legacy conversion that loses its row lock can delete a hook that another conversion just converted. This needs the set of legacy hooks to change in between. The next visit reinstalls, and deliveries in between are lost.
- During a conversion, deliveries are refused with 403 for as long as the edit and deletes take, once per row.
Dependencies
- Blocked on the R1 schema migration: the claim columns,
gitlab_hook_intentandgitlab_delivery. It has to come after revision 175, so after feat(sandbox): record lifecycle state on the sandbox row #599. - Relies on the R1 bootstrap having fenced the legacy CronJobs and recorded a committed intent for every row's existing uuid. That bootstrap needs the R0 credential split.
- Part of the Procrastinate migration (OHE-3417), which lands this alongside the maintenance (Maintenance task queue: unlocked claim lets two runners take the same task, and a crashed task blocks budget maintenance for up to an hour #552) and Resend (resend-sync marks a user synced before adding them, so an interrupted run loses that user permanently #553) claim fixes.
This issue was drafted by an AI agent on behalf of the user.
Activity
fix(gitlab): PLTF-3689 claim webhook rows so overlapping installer ru…
fix(gitlab): PLTF-3689 claim webhook rows and support hook repair (#692)
aivong-openhands commentedon Oct 19, 2026
Status update
Partly fixed. #681 and #692 claim rows per run and keep a row's credentials across attempts. #874 can now run the job on the procrastinate worker (off by default). Hook identity, automatic repair, legacy conversion and durable delivery dedupe aren't built yet.
Done:
claim_run_id,claimed_at,reinstall_requested_genandreinstall_done_gentogitlab_webhook.GitlabWebhookStore.claim_rowsclaims up to 100 rows withFOR UPDATE SKIP LOCKEDand stamps each with the run's ID. The lease is 10 minutes.claim_run_id. A run that lost its claim writes nothing and logsgitlab_webhook.stale_write_rejected.install_claimed_webhookwrites the row's uuid and secret before the create and keeps them across attempts. If a create succeeded but its response was lost, the hook still authenticates and the next run adopts it. This replaces the "new intent, delete the first hook" approach in the proposed fix.python -m sync.repair_gitlab_hooks --project|--group <id>asks the installer to delete every hook with our URL on the resource and create one. It's manual only; nothing runs it automatically.install_gitlab_webhooksperiodic task (* * * * *). Runs don't overlap because of a procrastinatelockandqueueing_lock. The task is off unlessOH_WORKER_SCHEDULED_JOBS_INSTALL_GITLAB_WEBHOOKS=true, so Kubernetes installs keep the CronJob andconcurrencyPolicy: Forbid.Still open:
?oh_hook=<uuid>URL and nogitlab_hook_intenttable.gitlab_deliverytable.POST /gitlab/reinstall-webhookcallsreset_webhook_for_reinstallation_by_resource(setswebhook_exists=false,webhook_uuid=NULL). It then callsinstall_webhook_on_resource, which writes new credentials unconditionally. A scheduled run can claim the same row at the same time and install its own hook. The result is two hooks, and the one whose credentials were overwritten gets 403 on every delivery.refresh_claimand the create, and whose claim is taken over, can still create a second hook. Both hooks carry the row's credentials, so both authenticate, and only the 60-second Redis key stops an event from being processed twice. Under the worker, this needs a run that housekeeping ended as stalled while its process kept running.CLAIM_LEASEsays it's "twice the job's 300 s deadline". Neither the chart'sgitlab-webhook-installCronJob nor the worker task sets a deadline. The impact is small, because the claim is refreshed before each create, but the comment should be fixed or a deadline added.Items 1–4 may be worth re-scoping now that #874 serializes runs. Items 5 and 6 still let duplicate hooks or duplicate deliveries happen without them.
Acceptance criteria:
Dependencies: the claim columns landed in migration 177 (#681).
gitlab_hook_intentandgitlab_deliveryhaven't been added.This comment was created by an AI agent (OpenHands) on behalf of @aivong-openhands, after reviewing
mainat 3590801.