p-queue CAPTCHA Solving in Node.js: Concurrency and Timeouts

For a p-queue captcha job in Node.js, create the queue with a concurrency that starts at the Max. Threads value in CapSkip, call solver.recaptcha() inside each queued job and submit the form in that same job, so every token is used right after it is solved. Most of this guide covers what goes wrong around it: two timeouts on different clocks, a queue that forgets a solve it gave up on, and one rejected job that ends the process. The CapSkip Node.js CAPTCHA solver does the solving on your own Windows PC, at no fee per solve.
What you need
- A Windows machine with CapSkip running, and the Max. Threads value of its reCAPTCHA row at hand.
- Node.js 22 or newer: the engines field of p-queue 10.0.0 says 22 and that of capskip 1.8.0 says 18. I ran the script on 24.22.0 and 25.2.1, never on 22 itself.
- A page with a reCAPTCHA v2 form that you have permission to automate. The reCAPTCHA v2 guide for Node.js shows where its sitekey sits in the page.
- A way to reach CapSkip. Local mode keeps the API on 127.0.0.1, while Server mode lets other machines call it. The setup guide explains both under Connection Settings.
The numbers below were measured against two small servers of mine, one standing in for the CapSkip API and one for the sign-up site, so the site rules are mine. The runs used Node.js 24.22.0 on Windows 11 with capskip 1.8.0 and p-queue 10.0.0. The mock solves in 2 to 4 seconds, far faster than a real reCAPTCHA solve, so read the figures as shapes. The real app wasn’t answering on this PC, so I sent it nothing. Claims resting on the setup guide or package source are labelled.
Step 1: install both packages and import them
The capskip package is CommonJS and p-queue 10 is ES module only, and that decides how each file imports them. In a project whose package.json sets type to module, plain import lines work for both. Run the first block in an empty folder.
# Run in an empty folder, in PowerShell npm init -y npm pkg set type=module npm install capskip p-queue
I ran these on three Node.js versions, in that project and in a second one made by npm init -y, which writes type commonjs. 20.11.1 is below the p-queue floor.
| Where and what | 20.11.1 | 24.22.0 and 25.2.1 |
|---|---|---|
| Module project, import of capskip and p-queue | Works | Works |
| CommonJS project, require of p-queue | ERR_REQUIRE_ESM | Loads, but as a namespace object, so new fails with "not a constructor" |
| CommonJS project, import() of p-queue | Works | Works |
| CommonJS project with type commonjs, import line in a .js file | SyntaxError | SyntaxError |
| Module project, require of capskip | ReferenceError | ReferenceError |
Without a type field, Node.js 24 and 25 ran that import line as a module, with a warning. So a CommonJS file loads p-queue with import() inside an async function, as below, which prints function 2 on all three versions.
// queue_check.cjs
// The same two packages in a CommonJS project, one without "type": "module".
const { CapSkip } = require("capskip");
async function main() {
// p-queue 10 is ESM only, so a CommonJS file loads it with import().
const { default: PQueue } = await import("p-queue");
const queue = new PQueue({ concurrency: 2 });
const solver = new CapSkip({ host: "127.0.0.1", port: 8080 });
console.log(typeof solver.recaptcha, queue.concurrency);
}
main();Step 2: write the job and the queue
Save the next three blocks as one file, queue_solve.mjs, in the step 1 folder, in order, with your own URLs and sitekey. The constants at the top carry the main lesson: capskip counts in seconds, p-queue in milliseconds, and the two timeouts start at different moments. SDK_TIMEOUT is 300, the capskip default for recaptcha(), because the setup guide gives the Row Timeout of a reCAPTCHA row as 250 seconds, with queue time on top.
// queue_solve.mjs
// npm install capskip p-queue
import PQueue, { TimeoutError } from "p-queue";
import {
CapSkip, ApiException, NetworkException, TimeoutException, ValidationException,
} from "capskip";
const PAGE_URL = "https://example.com/signup"; // the page that shows the widget
const POST_URL = "https://example.com/api/signup"; // where its form sends the answer
const SITEKEY = "YOUR_SITEKEY";
const MAX_THREADS = 3; // the Max. Threads value of the reCAPTCHA row in CapSkip
const SDK_TIMEOUT = 300; // capskip: seconds, counted from the moment CapSkip accepts the task
const POLL = 5; // capskip: the longest pause between two polls, in seconds
// p-queue: milliseconds, counted from the moment the job starts running. capskip
// checks its deadline only at a poll, so it can end up to POLL seconds late.
// Give p-queue that much room and a margin, so the SDK gives up first.
const JOB_TIMEOUT = (SDK_TIMEOUT + POLL + 5) * 1000;
// Local mode. In Server mode, use the address of the PC that runs CapSkip.
const solver = new CapSkip({ host: "127.0.0.1", port: 8080 });
const t0 = Date.now();
const secs = () => ((Date.now() - t0) / 1000).toFixed(1);A job is one fresh solve and one form post, because a token works once. fetch sends neither Referer nor Origin, so the job sets both, since a CSRF check on your site may want them.
// One job: one fresh solve, then one form post that carries that token.
async function signUp(email, signal) {
const started = secs();
const result = await solver.recaptcha(SITEKEY, PAGE_URL, {
timeout: SDK_TIMEOUT,
polling_interval: POLL,
});
const res = await fetch(POST_URL, {
method: "POST",
// fetch sends neither header by itself, and a CSRF check may want both.
headers: { Referer: PAGE_URL, Origin: new URL(PAGE_URL).origin },
body: new URLSearchParams({ email, "g-recaptcha-response": result.code }),
// If the queue gave up on this job while the solve ran, the signal is
// aborted and fetch rejects at once, so the token is not spent.
signal,
});
if (!res.ok) throw new Error(`the site answered HTTP ${res.status}`);
return `started ${started} s, finished ${secs()} s`;
}
function describe(err) {
if (err instanceof TimeoutError) return `p-queue gave up: ${err.message}`;
if (err instanceof TimeoutException) return `capskip stopped polling: ${err.message}`;
if (err instanceof ValidationException) return `refused before sending: ${err.message}`;
if (err instanceof NetworkException) return `CapSkip not reachable: ${err.message}`;
if (err instanceof ApiException) return `CapSkip answered an error: ${err.message}`;
return err instanceof Error ? err.message : String(err);
}The loop below waits on onSizeLessThan before each add, and it handles every rejection on the spot, because the promise that add returns rejects when the job fails. Run it with node queue_solve.mjs. It prints one line per email and a last line with the queue counters.
const queue = new PQueue({ concurrency: MAX_THREADS, timeout: JOB_TIMEOUT });
// Ctrl+C aborts this controller. Jobs still waiting are dropped without a solve.
const stop = new AbortController();
process.once("SIGINT", () => stop.abort(new Error("stopped with Ctrl+C")));
const emails = Array.from({ length: 12 }, (_, i) => `user${i + 1}@example.com`);
const lines = [];
let failed = 0;
for (const email of emails) {
if (stop.signal.aborted) break;
// Backpressure: wait here while six jobs are already waiting for a thread.
await queue.onSizeLessThan(MAX_THREADS * 2);
// Handle the rejection on the spot. A bare queue.add() that fails later is an
// unhandled rejection, and Node ends the process on one.
lines.push(
queue
.add(({ signal }) => signUp(email, signal), { signal: stop.signal })
.then((text) => `ok ${email} ${text}`)
.catch((err) => {
failed += 1;
return `FAIL ${email} ${describe(err)}`;
}),
);
}
(await Promise.all(lines)).forEach((line) => console.log(line));
console.log(`${queue.size} waiting, ${queue.pending} running, ${secs()} s in all`);
process.exitCode = failed === 0 ? 0 : 1;How many jobs should a p-queue captcha queue run at once?
According to the setup guide, Max. Threads is how many tasks of one type CapSkip solves at once, and the others wait in a queue inside the app. My mock follows that: a task beyond the limit waits after the app has accepted it. Whether the real app answers the submit call at once under load, I couldn’t test. These runs used three threads, a 2.0 second solve, 12 jobs and a capskip timeout that was never reached.
| Queue concurrency | All 12 jobs, polling_interval 5 | polling_interval 1 | Most tasks waiting inside the mock |
|---|---|---|---|
| 1 | 45.6 s | 33.6 s | 0 |
| 3 | 15.2 s | 11.2 s | 0 |
| 6 | 11.4 s | 8.7 s | 3 |
| 12 | 12.8 s | 8.9 s | 9 |
Going past Max. Threads never put more than three solves in progress. Yet a few extra jobs helped, and the reason is the SDK. Per its source, capskip polls after 0.25 seconds and doubles the pause up to polling_interval, so a job notices a finished solve late: a 2.0 second solve held its queue slot for 3.8 seconds with the default of 5 and for 2.8 seconds with 1. At concurrency 3 the mock’s threads idled in that gap. Extra jobs wait inside CapSkip with their capskip clock running, as the next section shows, and the app’s Wait Timeout can end them too. So start at Max. Threads, lower polling_interval, and add jobs only if the Monitor tab shows idle threads.
Two timeouts, two clocks
The capskip clock starts when CapSkip has accepted the task, not when it starts solving. In the source, solve sends the task, then waitResult sets its deadline, and the loop checks that deadline only at a poll. The p-queue clock starts when the job starts running, and queue time is not counted, which the p-queue README states. These rows are mock results.
| Setup | What happened |
|---|---|
| capskip timeout 3, polling_interval 1, solve 2.0 s, 3 threads, concurrency 3, 6 jobs | All six finished, 5.6 s in all |
| The same with concurrency 6 | Three finished at 2.8 s. The other three ended with TimeoutException, "timeout 3 exceeded", at 3.8 s, while their solves were still under way: the mock finished all 6 solves and 3 results were never read |
| p-queue timeout 3000 ms, concurrency 1, 3 jobs, solve 2.0 s, polling_interval 1 | All three finished, at 2.8, 5.6 and 8.4 s, though the timeout was 3.0 s |
| p-queue timeout 2000 ms, solve 4.0 s, 2 threads, concurrency 2, 6 jobs | All six rejected with TimeoutError, and the script’s last line came at 6.0 s |
| capskip timeout 2, polling_interval 1, otherwise as the row above | All six rejected with TimeoutException, and the script’s last line came at 8.4 s |
| capskip timeout 10, polling_interval 1, p-queue timeout 16 s from the formula in the script, otherwise as above | All six finished in about 14.5 s |
The second row is the capskip clock: three jobs that waited inside the mock ran out of time. The third is the p-queue clock: the last job finished far past its timeout, having waited most of it. The fourth and fifth rows are the trap. A timeout of either kind frees the queue slot and does nothing to the solve. The SDK has no call that cancels a task, and p-queue says a timed-out job keeps running unless it handles its signal. So the next job goes to a CapSkip still busy with the abandoned one, and the queue counters can’t see it: when the run with the short p-queue timeout printed 0 waiting and 0 running at 6.0 seconds, the mock still had 2 solves running and 2 waiting, and onIdle resolved at that same moment.
When p-queue gives up first, the SDK keeps polling in the background: the last line came at 6.0 seconds, but the process stayed open until 16.9 seconds, because six polling loops kept going. All 6 tokens were read and 0 were submitted. When the SDK gives up first, polling stops: the last line came at 8.4 seconds and the process ended at 8.5. The mock still finished 6 solves, and 0 tokens were read. So the script derives JOB_TIMEOUT from the capskip numbers, as the last row did: the timeout plus one polling pause plus a margin.
A note for older snippets: in p-queue 8.1.0 a timed-out job resolved with undefined unless throwOnTimeout was set. From 9.0.0, which I also ran, a timeout always rejects with a TimeoutError, and throwOnTimeout set to false changed nothing. The name appears nowhere in the 9.0.0 or 10.0.0 source.
Stopping, backpressure, rate limits and failures
Each add call takes a signal. If it aborts while a job waits, the job is removed and no solve is bought. If it aborts while the job runs, add rejects and the slot is freed, but the job goes on, which is the same trap. The script wires Ctrl+C to the controller, and because I can’t send an interrupt to a child process on Windows from a test, the run used a timer that calls the same abort at 4.0 seconds. With 12 jobs, three threads, a 2.0 second solve and polling_interval 1, three jobs had finished, three were running and six were waiting. All nine were rejected with the abort reason, and the mock received 6 solves instead of 12. The running jobs kept polling, so the process ended at 5.7 seconds, 1.7 seconds after the abort.
Passing the queue signal on to fetch is what keeps an abandoned job from submitting. With the signal left out of the fetch call, the three abandoned jobs still submitted their tokens at 5.6 seconds, and the site received 6 form posts instead of 3.
For backpressure, queue.size counts the jobs waiting and queue.pending the ones running, and onIdle resolves when both are zero. A producer that adds 60 jobs in a loop left 57 of them waiting, while one that awaits onSizeLessThan(6) never left more than 6, and both finished in about the same time (about 15.5 seconds each). An abort or a crash discards every waiting job, so keeping that number small also keeps the loss small.
The options interval and intervalCap limit how many jobs start in each interval. In a mock with 12 threads, a 0.5 second solve and concurrency 6, six form posts reached the site together at 0.8 seconds and six more at 1.6 without a limit. With intervalCap 2 and interval 1000 they arrived in pairs about a second apart, from 0.8 to 5.8 seconds. The cap counts job starts, so a form submission lands after the solve and the poll lag, not at the start. Use it for the target site, and read the guide to HTTP 429 for what to do when it answers anyway.
One failed job must not take the others with it. In a mock that fails the second task, with six jobs, the script as written printed 5 ok lines and one FAIL line, and exited with code 1 by design. The same loop with a bare add call crashed at 1.9 seconds with code 1 and the unhandled ApiException, before any form submission reached the site. One awaited addAll gave the caller the first error at 1.8 seconds and nothing more: the other five jobs still ran and submitted, with no outcome printed. Promise.allSettled over the add calls matched the handlers.
Common errors
A row marked seen was reproduced in a p-queue captcha run against my mock.
| Error | Cause | Fix |
|---|---|---|
| ERR_REQUIRE_ESM | A require of p-queue on Node.js 20.11.1 (seen) | Use import() |
| not a constructor | A require of p-queue returns a namespace object (seen) | Use the default export through import() |
| TimeoutError from p-queue | The job ran longer than its timeout, which counts run time only (seen) | Raise JOB_TIMEOUT above the capskip numbers |
| TimeoutException from capskip | No answer before the deadline, which includes the wait inside CapSkip (seen) | Lower concurrency or raise the timeout |
| ERROR_CAPTCHA_UNSOLVABLE | The task could not be solved, per the CapSkip docs. My mock produced it for one job | Run that job again |
| Process ends with code 1 | An add call rejected and nobody handled it (seen) | Add a catch to every add call |
| HTTP 403 from the site | My mock refuses a missing Referer or a reused token | Compare with the request the page sends |
FAQ
Can CapSkip run on another PC?
A p-queue captcha job can run on one PC and CapSkip on another. Put CapSkip in Server mode and set the host option of the client to that PC. Max. Threads still caps the solves, and the queue concurrency is still the number to match. The setup guide covers both modes. In Server mode CapSkip allows inbound connections on its port from any address, so a narrower allow rule of your own changes nothing. Add a Windows Firewall block rule for each machine you don’t expect, and turn on API Key Validation and pass your key as the apiKey option.
Does p-queue retry a failed solve?
No. Neither its options nor its source mention a retry. Add the job again yourself, which buys a fresh solve, since a token works once.
Should every CAPTCHA type share one queue?
No. The setup guide gives each type its own Max. Threads, so give each type its own queue and its own concurrency. For browser workers instead of plain requests, see the puppeteer-cluster guide.
A p-queue captcha job comes down to a concurrency near Max. Threads, a capskip timeout that covers the wait, a queue timeout above it and a catch on every add call. None of it costs a fee, because this captcha solver runs on your own PC, and the Node.js CAPTCHA solver page covers the other CAPTCHA types.
