Skip to content
HooktierDocs

GitHub webhooks: "Timed out" and "Invalid HTTP response" (502, 504)

GitHub lists failed deliveries under Recent deliveries. Its troubleshooting page has a section for each error:

  • Timed out: GitHub did not receive a response within 10 seconds.
  • Invalid HTTP response: your server, or a proxy in front of it, answered with a 4xx or 5xx status, for example 502 Bad Gateway or 504 Gateway Timeout.
  • Failed to connect to host and Failed to connect to network: GitHub could not reach your URL.
  • Peer certificate cannot be authenticated with given CA certificates: GitHub could not verify your TLS certificate.

GitHub’s documentation has no entry for 502 itself. It counts every 4xx and 5xx response as a failed delivery. The sections on 502 and 504 below explain where these codes usually come from.

GitHub does not automatically redeliver failed deliveries. A failed delivery may already have been processed if its response was lost or late. Redeliver failed deliveries with deduplication to avoid repeating completed work. This page explains each result, the fix, and how to recover the deliveries you missed.

GitHub expects a 2XX response within 10 seconds of a delivery. If your server takes longer, GitHub ends the connection and counts the delivery as a failure. A 4XX or 5XX response is also a failure.

GitHub Enterprise Server documents 30 seconds in some versions (3.18 to 3.20, for example). Check the documentation for your version if you run Enterprise Server.

The usual reason for a timeout is a handler that does the work before it answers: it calls the GitHub API, runs a build, clones a repository or writes to a slow database. Cold starts on serverless platforms and a server that is busy with other requests add to that time.

GitHub’s documentation suggests a queue: your server answers when it receives the webhook, then processes the payload in the background. The handler does three things:

  1. Verify the X-Hub-Signature-256 header.
  2. Store the payload or put it on a queue.
  3. Answer 202 Accepted (any 2XX works).

A worker then reads from the queue and does the slow work.

The queue in this sample is an in-memory channel, which keeps the sample short. It loses queued deliveries when the process stops. In production, write the delivery to a database table or a durable queue before you answer.

package main
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"io"
"log"
"net/http"
"os"
"strings"
)
type delivery struct {
id, event string
body []byte
}
var queue = make(chan delivery, 1000)
func validGitHubSignature(body []byte, header, secret string) bool {
// An empty key would let anyone compute a valid signature.
if secret == "" {
return false
}
given, ok := strings.CutPrefix(header, "sha256=")
if !ok {
return false
}
sig, err := hex.DecodeString(given)
if err != nil {
return false
}
mac := hmac.New(sha256.New, []byte(secret))
mac.Write(body)
return hmac.Equal(sig, mac.Sum(nil))
}
func githubWebhook(w http.ResponseWriter, r *http.Request) {
// GitHub caps payloads at 25 MB.
body, err := io.ReadAll(http.MaxBytesReader(w, r.Body, 25<<20))
if err != nil {
http.Error(w, "cannot read body", http.StatusBadRequest)
return
}
if !validGitHubSignature(body, r.Header.Get("X-Hub-Signature-256"), os.Getenv("GITHUB_WEBHOOK_SECRET")) {
http.Error(w, "invalid signature", http.StatusUnauthorized)
return
}
d := delivery{id: r.Header.Get("X-GitHub-Delivery"), event: r.Header.Get("X-GitHub-Event"), body: body}
select {
case queue <- d:
w.WriteHeader(http.StatusAccepted)
default:
// The queue is full: fail now, so the delivery shows as failed and can be redelivered.
http.Error(w, "queue full", http.StatusServiceUnavailable)
}
}
func worker() {
for d := range queue {
// The slow work happens here, after GitHub has its response.
log.Printf("processing %s %s (%d bytes)", d.event, d.id, len(d.body))
}
}
func main() {
go worker()
http.HandleFunc("POST /webhooks/github", githubWebhook)
log.Fatal(http.ListenAndServe(":3000", nil))
}
import crypto from "node:crypto";
import express from "express";
const app = express();
const secret = process.env.GITHUB_WEBHOOK_SECRET;
const queue = []; // in-memory for the sample; use a durable queue in production
function validGitHubSignature(rawBody, header) {
// An empty key would let anyone compute a valid signature.
if (typeof secret !== "string" || secret.length === 0) return false;
if (typeof header !== "string" || !/^sha256=[0-9a-f]{64}$/.test(header)) return false;
const expected = crypto.createHmac("sha256", secret).update(rawBody).digest();
const given = Buffer.from(header.slice("sha256=".length), "hex");
return given.length === expected.length && crypto.timingSafeEqual(given, expected);
}
app.post(
"/webhooks/github",
express.raw({ type: "application/json", limit: "25mb" }),
(req, res) => {
if (!validGitHubSignature(req.body, req.get("X-Hub-Signature-256"))) {
return res.sendStatus(401);
}
queue.push({ id: req.get("X-GitHub-Delivery"), event: req.get("X-GitHub-Event"), body: req.body });
res.sendStatus(202); // answer now
},
);
// A worker loop takes deliveries off the queue and does the slow work.
setInterval(() => {
const d = queue.shift();
if (d) console.log(`processing ${d.event} ${d.id} (${d.body.length} bytes)`);
}, 100);
app.listen(3000);

Both samples expect the webhook’s content type application/json, which you set in the webhook settings on GitHub. With application/x-www-form-urlencoded, the JSON arrives in a form field named payload, and these samples do not parse it.

The delivery details show the response that GitHub received from your server. When your app runs behind nginx, a load balancer, Cloudflare or a platform router, a 502 or 504 usually comes from that proxy, not from your code. The response body in Recent deliveries often names the proxy.

The proxy could not get a valid response from your app. Common reasons:

  • The app is not running or not listening on the port the proxy uses, for example during a deploy or after a crash.
  • The app closed the connection before it answered, for example when the process ran out of memory while it handled a large payload.
  • The proxy points to the wrong upstream address or port.

Check the proxy’s error log at the delivery time. Typical nginx lines for these cases look like connect() failed (111: Connection refused) while connecting to upstream and upstream prematurely closed connection while reading response header from upstream. Then check the app’s own log and its restarts at the same time.

A proxy waited for your app and gave up. A recorded 504 means GitHub received that response before its own timeout. Check the upstream logs to identify which component generated it. The status alone does not reveal a proxy’s configured timeout. (nginx’s proxy_read_timeout, the longest wait between two reads from the app, is 60 seconds by default.)

Raising a proxy timeout does not fix a slow handler, because GitHub still stops after 10 seconds. Make the handler answer within 10 seconds, as shown above.

Cloudflare uses its own 5XX codes when it cannot reach your origin, for example 521 (the origin web server refuses connections) and 522 (the TCP connection to the origin timed out). Its 524 code (a timeout, by default after 125 seconds without a response, or a proxy write timeout) does not appear in GitHub’s delivery log when GitHub stops waiting first. If Cloudflare’s security features challenge or block GitHub’s requests, the log shows a 403 with a Cloudflare page in the response body. Allow GitHub’s webhook addresses: use the hooks list from GET https://api.github.com/meta (GitHub’s documentation points to /meta for its IP addresses).

A proxy can also change the body or headers. GitHub’s troubleshooting page warns about this for signature checks: a proxy or load balancer must not modify the payload or the headers.

GitHub could not open a connection to your URL. Check that:

  • the URL resolves in public DNS and is reachable from the internet,
  • a firewall allows GitHub’s addresses (the hooks list from GET https://api.github.com/meta).

Peer certificate cannot be authenticated with given CA certificates

Section titled “Peer certificate cannot be authenticated with given CA certificates”

GitHub could not verify your server’s TLS certificate. Check that the certificate is valid, is not self-signed, and that the server sends the intermediate certificates (the full chain).

GitHub does not redeliver on its own. You can redeliver a delivery from the past three days, in the web interface or with the REST API.

In the web interface, for a repository webhook:

  1. Open the repository settings, then Webhooks, and select the webhook.
  2. Click Recent deliveries. All deliveries from the past three days are listed.
  3. Click the delivery GUID of the failed delivery.
  4. Click Redeliver.

For a GitHub App, the deliveries are in the app’s settings under Advanced, then Recent deliveries.

With the REST API and the GitHub CLI, for a repository webhook:

Terminal window
# List recent deliveries of webhook HOOK_ID with their status.
gh api "repos/OWNER/REPO/hooks/HOOK_ID/deliveries?per_page=100" \
--jq '.[] | [.id, .guid, .status_code, .status, .event, .delivered_at] | @tsv'
# Only failed deliveries (status codes 400 to 599): add &status=failure to the URL above.
# Redeliver one delivery by its ID.
gh api --method POST "repos/OWNER/REPO/hooks/HOOK_ID/deliveries/DELIVERY_ID/attempts"

Organization webhooks use orgs/ORG/hooks/HOOK_ID/deliveries, and GitHub App webhooks use app/hook/deliveries with a JSON Web Token for the app. GitHub’s documentation has complete scripts that find failed deliveries and redeliver them on a schedule.

A redelivered request keeps the X-GitHub-Delivery value of the original delivery. Store the IDs you have processed and skip an ID you have seen, so a redelivery never does the work twice.

  1. The handler answers 2XX within 10 seconds, and the slow work runs from a queue.
  2. A full queue or a failed write answers 5XX, so the delivery shows as failed and you can redeliver it.
  3. The proxy’s error log and the app’s restarts explain each 502 or 504.
  4. Cloudflare and firewalls allow GitHub’s hooks addresses.
  5. A scheduled job redelivers failed deliveries before they are three days old.
  6. The worker skips X-GitHub-Delivery values it has already processed.

Checked on 2026-10-10.