Node.js based forum software built for the modern web
refactor(activitypub): replace bespoke worker pool with workerpool
Julian Lam5eeeb9dcd330
diff --git a/src/activitypub/index.js b/src/activitypub/index.jsindex 6724c29c04..1fea8a133b 100644--- a/src/activitypub/index.js+++ b/src/activitypub/index.js@@ -443,7 +443,7 @@ ActivityPub.send = async (type, id, targets, payload) => {}// Start the drain loop if not already draining- if (SendPool.pool.length > 0) {+ if (SendPool.pool > 0) {SendPool.drainLoop();}});@@ -683,7 +683,7 @@ ActivityPub.probe = async ({ uid, url }) => {// ---------------------------------------------------------------------------ActivityPub.shutdown = function () {- if (SendPool.pool.length > 0) {+ if (SendPool.pool > 0) {SendPool.shutdown();}};diff --git a/src/activitypub/send.js b/src/activitypub/send.jsindex 050c6af190..25a7c98ef3 100644--- a/src/activitypub/send.js+++ b/src/activitypub/send.js@@ -2,167 +2,71 @@const os = require('os');const path = require('path');-const { fork } = require('child_process');-const { createHash } = require('crypto');const winston = require('winston');+const workerpool = require('workerpool');const db = require('../database');+// ---------------------------------------------------------------------------+// Worker pool — managed by workerpool+// ---------------------------------------------------------------------------+const SendPool = {- pool: [],- free: [],- busy: new Map(),- pending: new Map(),- taskTimers: new Map(),- inFlight: new Set(),- draining: false,- isShuttingDown: false,- maxWorkers: 0,+ _activityPub: null,};-SendPool.maxWorkers = Math.max(4, os.availableParallelism() * 2);+SendPool._pool = workerpool.pool(+ path.join(__dirname, 'sendWorker.js'),+ {+ minWorkers: Math.max(4, os.availableParallelism() * 2),+ maxWorkers: Math.max(4, os.availableParallelism() * 2),+ workerType: 'process',+ forkOpts: { silent: true },+ },+);++Object.defineProperty(SendPool, 'pool', {+ get() {+ return this._pool?.workers?.length || 0;+ },+});SendPool.init = function (activityPub) {if (activityPub) {SendPool._activityPub = activityPub;}- for (let i = 0; i < SendPool.maxWorkers; i++) {- SendPool.forkWorker();- }-};--SendPool.forkWorker = function () {- const workerPath = path.join(__dirname, 'sendWorker.js');- const proc = fork(workerPath, [], {- silent: true,- env: { AP_SEND_CHILD: 'true' },- });-- proc.once('exit', (code) => {- SendPool.handleWorkerExit(proc, code);- });-- proc.on('message', (message) => {- if (!message || typeof message.type !== 'string') {- return;- }-- switch (message.type) {- case 'ready': {- // Worker is ready to accept tasks- if (!SendPool.free.includes(proc)) {- SendPool.free.push(proc);- }- break;- }-- case 'result': {- SendPool.handleResult(message);- break;- }-- case 'ack': {- // Shutdown acknowledged — worker will exit- break;- }-- default:- winston.warn(`[activitypub/send] Unknown worker message: ${message.type}`);- break;- }- });-- proc.on('error', (err) => {- winston.error(`[activitypub/send] Worker error: ${err.message}`);- });-- SendPool.pool.push(proc);-};--SendPool.handleWorkerExit = function (proc, code) {- winston.warn(`[activitypub/send] Worker exited with code ${code}`);-- // Remove from pool and free arrays- const poolIdx = SendPool.pool.indexOf(proc);- if (poolIdx !== -1) {- SendPool.pool.splice(poolIdx, 1);- }- const freeIdx = SendPool.free.indexOf(proc);- if (freeIdx !== -1) {- SendPool.free.splice(freeIdx, 1);- }-- // Re-queue in-flight task if this worker had one- const taskId = SendPool.busy.get(proc);- if (taskId) {- const task = SendPool.pending.get(taskId);- if (task) {- // Re-queue immediately — the worker crashed mid-flight- SendPool.requeueTask(task);- }- SendPool.busy.delete(proc);- SendPool.pending.delete(taskId);- SendPool.clearTaskTimer(taskId);- if (task) {- SendPool.inFlight.delete(task.queueId);- }- }-- // Fork a replacement worker only if not shutting down- if (!SendPool.isShuttingDown) {- SendPool.forkWorker();- }};-SendPool.handleResult = function (message) {- const { id, success, error } = message;- const task = SendPool.pending.get(id);-- // Clear the stuck-task timer- SendPool.clearTaskTimer(id);+// ---------------------------------------------------------------------------+// Result handler — called after worker completes a task+// ---------------------------------------------------------------------------- // Remove from pending and busy tracking- SendPool.pending.delete(id);-- if (!task) {- return;- }-- // Find which worker handled this task, mark it free, and return to pool- for (const [worker, taskId] of SendPool.busy) {- if (taskId === id) {- SendPool.busy.delete(worker);- if (!SendPool.free.includes(worker)) {- SendPool.free.push(worker);- }- break;- }- }-- if (success) {+SendPool.handleResult = function (queueId, result) {+ if (result.success) {// Success — fire analytics and remove from RedisSendPool._activityPub.analytics.send({- type: task.payloadType,- target: task.uri,+ type: result.payloadType,+ target: result.uri,});- db.delete(`ap:retry:queue:${task.queueId}`);- db.sortedSetRemove('ap:retry:queue', task.queueId);- SendPool.inFlight.delete(task.queueId);+ db.delete(`ap:retry:queue:${queueId}`);+ db.sortedSetRemove('ap:retry:queue', queueId);} else {try {SendPool._activityPub.analytics.sendError({- payload: JSON.parse(task.payload),- uri: task.uri,- error: new Error(error),+ payload: JSON.parse(result.payload),+ uri: result.uri,+ error: new Error(result.error),});} catch (e) {- winston.warn(`[activitypub/send] Task ${id} failed: ${error}`);+ winston.warn(`[activitypub/send] Task failed: ${result.error}`);}- SendPool.inFlight.delete(task.queueId);- SendPool.requeueTask(task);}};+// ---------------------------------------------------------------------------+// Retry queue — exponential backoff into Redis+// ---------------------------------------------------------------------------+SendPool.requeueTask = function (task) {const oneMinute = 1000 * 60;const maxDelay = 60 * 60 * 1000; // 1 hour@@ -196,68 +100,40 @@ SendPool.requeueTask = function (task) {db.setObjectBulk(retryQueuedSet);};-SendPool.clearTaskTimer = function (taskId) {- const timer = SendPool.taskTimers.get(taskId);- if (timer) {- clearTimeout(timer);- SendPool.taskTimers.delete(taskId);- }-};--SendPool.dispatch = function (taskId, task) {- if (SendPool.free.length === 0) {- // No free workers — task stays in pending queue- return false;- }-- const worker = SendPool.free.shift();- SendPool.busy.set(worker, taskId);-- // Set 30s stuck detection timer- const timer = setTimeout(() => {- // Task has been in-flight for >30s — re-queue and kill worker- SendPool.clearTaskTimer(taskId);- SendPool.pending.delete(taskId);- SendPool.inFlight.delete(task.queueId);- SendPool.requeueTask(task);- worker.kill('SIGKILL');- }, 30000);- SendPool.taskTimers.set(taskId, timer);-- try {- worker.send({- type: 'send',- id: taskId,- uri: task.uri,- payload: task.payload,- digest: task.digest,- key: task.key,- keyId: task.keyId,- });- } catch (err) {- // Worker disconnected — kill it, re-queue task- SendPool.busy.delete(worker);- SendPool.pending.delete(taskId);- SendPool.inFlight.delete(task.queueId);- SendPool.clearTaskTimer(taskId);- SendPool.requeueTask(task);- try {- worker.kill('SIGKILL');- } catch (e) { /* already dead */ }- }+// ---------------------------------------------------------------------------+// Dispatch — send task to workerpool+// ---------------------------------------------------------------------------- return true;+SendPool.dispatch = function (task) {+ // workerpool handles queuing automatically when all workers are busy.+ // The 30s timeout on exec kills the worker and rejects the promise,+ // which is caught by drainLoop and triggers re-queuing.+ return SendPool._pool.exec('send', [{+ id: task.id,+ uri: task.uri,+ payload: task.payload,+ digest: task.digest,+ key: task.key,+ keyId: task.keyId,+ }], { timeout: 30000 })+ .then(result => result)+ .catch(e => ({ success: false, error: e.message }));};+// ---------------------------------------------------------------------------+// Drain loop — fetch due tasks from Redis and dispatch+// ---------------------------------------------------------------------------+SendPool.drainLoop = async function () {- if (SendPool.draining) {+ if (SendPool._draining) {return;}- SendPool.draining = true;+ SendPool._draining = true;+ SendPool._inFlight = SendPool._inFlight || new Set();const MAX_PER_BATCH = 50;- while (SendPool.draining) {+ while (SendPool._draining) {try {// Get due tasks from Redis sorted setconst dueTasks = await db.getSortedSetRangeByScore(@@ -270,9 +146,9 @@ SendPool.drainLoop = async function () {if (dueTasks.length === 0) {// No tasks due — switch to idle mode- SendPool.draining = false;+ SendPool._draining = false;setTimeout(() => {- if (SendPool.pool.length > 0) {+ if (SendPool.pool > 0) {SendPool.drainLoop();}}, 10000); // 10-second idle timeout@@ -283,13 +159,14 @@ SendPool.drainLoop = async function () {const taskDataList = await Promise.all(dueTasks.map(queueId => db.getObject(`ap:retry:queue:${queueId}`)),);- // Pair each queueId with its own task data (taskDataList is aligned- // with the unfiltered dueTasks array) and skip in-flight or missing tasks++ // Pair each queueId with its own task data and skip in-flight or+ // missing tasksconst validTaskData = [];for (let i = 0; i < dueTasks.length; i++) {const queueId = dueTasks[i];const taskData = taskDataList[i];- if (taskData && !SendPool.inFlight.has(queueId)) {+ if (taskData && !SendPool._inFlight.has(queueId)) {validTaskData.push({ queueId, taskData });}}@@ -300,12 +177,11 @@ SendPool.drainLoop = async function () {));const keyResults = await Promise.all(keyPromises);- // Dispatch each task+ // Dispatch each task to workerpool (auto-queues if busy)for (let i = 0; i < validTaskData.length; i++) {const { queueId, taskData } = validTaskData[i];const keyData = keyResults[i];- const taskId = createHash('sha256').update(`dispatch:${queueId}`).digest('hex');const task = {queueId,uri: taskData.uri,@@ -318,33 +194,51 @@ SendPool.drainLoop = async function () {attempts: taskData.attempts || 1,};- SendPool.pending.set(taskId, task);- SendPool.inFlight.add(task.queueId);-- // Try to dispatch — if no free worker, leave in pending- const dispatched = SendPool.dispatch(taskId, task);- if (!dispatched) {- // No workers available — task stays in pending for next iteration- SendPool.pending.delete(taskId);- SendPool.inFlight.delete(task.queueId);- break;- }+ SendPool._inFlight.add(task.queueId);++ SendPool.dispatch(task)+ .then((result) => {+ SendPool._inFlight.delete(task.queueId);+ if (result.success) {+ SendPool.handleResult(queueId, {+ ...task,+ success: true,+ });+ } else {+ SendPool.handleResult(queueId, {+ ...task,+ success: false,+ error: result.error,+ });+ SendPool.requeueTask(task);+ }+ })+ .catch((e) => {+ // Exec rejected (e.g., worker crash, queue full)+ SendPool._inFlight.delete(task.queueId);+ SendPool.handleResult(queueId, {+ ...task,+ success: false,+ error: e.message || 'unknown error',+ });+ SendPool.requeueTask(task);+ });}// Yield to event loop between batches to prevent starvationif (dueTasks.length >= MAX_PER_BATCH) {setImmediate(() => {- if (SendPool.pool.length > 0) {+ if (SendPool.pool > 0) {SendPool.drainLoop();}});}} catch (e) {winston.error(`[activitypub/send] drainLoop error: ${e.message}`);- SendPool.draining = false;+ SendPool._draining = false;// Schedule retry after a delaysetTimeout(() => {- if (SendPool.pool.length > 0) {+ if (SendPool.pool > 0) {SendPool.drainLoop();}}, 10000);@@ -352,35 +246,24 @@ SendPool.drainLoop = async function () {}};-SendPool.shutdown = function () {- SendPool.isShuttingDown = true;- SendPool.draining = false;+// ---------------------------------------------------------------------------+// Shutdown — graceful termination with force-fallback+// ---------------------------------------------------------------------------- // Send shutdown to all workers- for (const worker of SendPool.pool) {- try {- worker.send({ type: 'shutdown' });- } catch (e) { /* worker may already be disconnected */ }- }+SendPool.shutdown = function () {+ SendPool._draining = false;- // Wait for workers to exit (10s timeout)- const shutdownTimeout = setTimeout(() => {- for (const worker of SendPool.pool) {- try {- worker.kill('SIGTERM');- } catch (e) { /* already dead */ }- }- // Second kill after 2s- setTimeout(() => {- for (const worker of SendPool.pool) {- try {- worker.kill('SIGKILL');- } catch (e) { /* already dead */ }- }- }, 2000);- }, 10000);+ // Graceful shutdown — let workers finish current tasks+ SendPool._pool.terminate(false, 10000).catch(() => {+ winston.warn('[activitypub/send] Workers did not exit gracefully');+ });- shutdownTimeout.unref();+ // Force kill workers if they haven't exited within the timeout+ setTimeout(() => {+ SendPool._pool.terminate(true, 10000).catch(() => {+ winston.warn('[activitypub/send] Force shutdown failed');+ });+ }, 12000);};module.exports = SendPool;diff --git a/src/activitypub/sendWorker.js b/src/activitypub/sendWorker.jsindex 84d57654f8..3121d08fab 100644--- a/src/activitypub/sendWorker.js+++ b/src/activitypub/sendWorker.js@@ -1,12 +1,10 @@'use strict';/**- * ActivityPub outbound send worker (child process).+ * ActivityPub outbound send worker.*- * Forked from index.js. Receives send tasks via IPC, signs payloads,- * POSTs to remote inboxes, and reports results back to the parent.- *- * Environment variable: AP_SEND_CHILD=true+ * Dedicated worker for workerpool — receives send tasks, signs payloads,+ * POSTs to remote inboxes, and returns results as Promises.*/const { fetch, Agent } = require('undici');@@ -15,9 +13,6 @@ const winston = require('winston');const nconf = require('nconf');const { version } = require('../../package.json');-const DEBUG = process.env.AP_SEND_DEBUG === 'true';-const userAgent = `NodeBB/${version.split('.').shift()}.x (${nconf.get('url')})`;-const {importPrivateKey,genDraftSigningString,@@ -93,170 +88,81 @@ async function sign(keyPem, keyId, url, digest) {}// ----------------------------------------------------------------------------// Worker message handler+// Worker task — exported to workerpool// ----------------------------------------------------------------------------// Track in-flight task IDs for uncaughtException handler-const pendingTaskIds = new Set();--// Track active task AbortController instances for immediate shutdown abort-const activeTasks = new Map(); // id -> AbortController+async function send({ id, uri, payload, digest, key, keyId }) {+ const userAgent = `NodeBB/${version.split('.').shift()}.x (${nconf.get('url')})`;+ const DEBUG = process.env.AP_SEND_DEBUG === 'true';-process.on('message', async (message) => {- if (!message || typeof message.type !== 'string') {- return;- }-- // Handle shutdown before the switch — avoids fallthrough eslint issue- // and ensures immediate exit without waiting for switch dispatch- if (message.type === 'shutdown') {- for (const [, controller] of activeTasks) {- controller.abort();+ try {+ // Validate required fields+ if (!uri || !payload || !digest || !key || !keyId) {+ return { id, success: false, error: 'missing fields' };}- process.send({ type: 'ack' });- process.exit(0);- }-- switch (message.type) {- case 'send': {- const { id, uri, payload, digest, key, keyId } = message;-- // Validate required fields- if (!uri || !payload || !digest || !key || !keyId) {- process.send({- type: 'result',- id,- success: false,- error: 'missing fields',- });- return;- }-- pendingTaskIds.add(id);-- // Create AbortController for this task — allows immediate abort on shutdown- const controller = new AbortController();- activeTasks.set(id, controller);-- try {- // SSRF check- const { ok } = await check(uri);- if (!ok) {- process.send({- type: 'result',- id,- success: false,- error: 'SSRF check failed — reserved IP address',- });- pendingTaskIds.delete(id);- activeTasks.delete(id);- return;- }-- // Sign- const headers = await sign(key, keyId, uri, digest);- // Debug: log full request details- if (DEBUG) {- winston.verbose(`[activitypub/send] REQUEST uri=${uri}`);- winston.verbose(`[activitypub/send] REQUEST headers=${JSON.stringify(headers)}`);- winston.verbose(`[activitypub/send] REQUEST payload=${payload.substring(0, 500)}`);- }+ // SSRF check+ const { ok } = await check(uri);+ if (!ok) {+ return { id, success: false, error: 'SSRF check failed — reserved IP address' };+ }- // POST — redirect: 'manual' prevents SSRF via HTTP redirect- // Combined signal: task-specific abort (shutdown) + 10s timeout- const timeoutSignal = AbortSignal.timeout(10000);- const combinedSignal = AbortSignal.any([controller.signal, timeoutSignal]);+ // Sign+ const headers = await sign(key, keyId, uri, digest);- const response = await fetch(uri, {- method: 'POST',- headers: {- ...headers,- 'content-type': 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',- 'user-agent': userAgent,- },- body: payload,- signal: combinedSignal,- redirect: 'manual',- dispatcher: agent,- });+ // Debug: log full request details+ if (DEBUG) {+ winston.verbose(`[activitypub/send] REQUEST uri=${uri}`);+ winston.verbose(`[activitypub/send] REQUEST headers=${JSON.stringify(headers)}`);+ winston.verbose(`[activitypub/send] REQUEST payload=${payload.substring(0, 500)}`);+ }- // Validate Content-Length to prevent memory exhaustion- const contentLength = response.headers.get('content-length');- if (contentLength && parseInt(contentLength, 10) > MAX_BODY_SIZE) {- process.send({- type: 'result',- id,- success: false,- error: 'Response body exceeds 10MB limit',- });- activeTasks.delete(id);- pendingTaskIds.delete(id);- return;- }+ // POST — redirect: 'manual' prevents SSRF via HTTP redirect+ // 10s timeout to prevent stuck tasks+ const timeoutSignal = AbortSignal.timeout(10000);++ const response = await fetch(uri, {+ method: 'POST',+ headers: {+ ...headers,+ 'content-type': 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',+ 'user-agent': userAgent,+ },+ body: payload,+ signal: timeoutSignal,+ redirect: 'manual',+ dispatcher: agent,+ });++ // Validate Content-Length to prevent memory exhaustion+ const contentLength = response.headers.get('content-length');+ if (contentLength && parseInt(contentLength, 10) > MAX_BODY_SIZE) {+ return { id, success: false, error: 'Response body exceeds 10MB limit' };+ }- if (String(response.status).startsWith('2')) {- process.send({- type: 'result',- id,- success: true,- });- } else {- let bodyText = '';- try {- bodyText = await response.text();- } catch (e) { /* ignore */ }+ if (String(response.status).startsWith('2')) {+ return { id, success: true };+ }- // Debug: log full response details- if (DEBUG) {- winston.verbose(`[activitypub/send] RESPONSE status=${response.status} headers=${JSON.stringify(Object.fromEntries(response.headers.entries()))}`);- winston.verbose(`[activitypub/send] RESPONSE body=${bodyText.substring(0, 1000)}`);- }+ let bodyText = '';+ try {+ bodyText = await response.text();+ } catch (e) { /* ignore */ }- process.send({- type: 'result',- id,- success: false,- error: `HTTP ${response.status}: ${bodyText}`,- });- }- } catch (e) {- process.send({- type: 'result',- id,- success: false,- error: e.message || 'unknown error',- });- } finally {- pendingTaskIds.delete(id);- activeTasks.delete(id);- }- break;+ // Debug: log full response details+ if (DEBUG) {+ winston.verbose(`[activitypub/send] RESPONSE status=${response.status} headers=${JSON.stringify(Object.fromEntries(response.headers.entries()))}`);+ winston.verbose(`[activitypub/send] RESPONSE body=${bodyText.substring(0, 1000)}`);}- default:- winston.warn(`[activitypub/send] Unknown message type: ${message.type}`);- break;+ return { id, success: false, error: `HTTP ${response.status}: ${bodyText}` };+ } catch (e) {+ return { id, success: false, error: e.message || 'unknown error' };}-});+}-// Uncaught exception handler — send error back if task ID is known, then exit-process.on('uncaughtException', (err) => {- if (pendingTaskIds.size > 0) {- // Send error for the first pending task (there should only be one)- const taskId = pendingTaskIds.values().next().value;- try {- process.send({- type: 'result',- id: taskId,- success: false,- error: `uncaughtException: ${err.message}`,- });- } catch (e) { /* IPC may be broken */ }- }- winston.error(`[activitypub/send] Uncaught exception: ${err.stack}`);- process.exit(1);-});+const workerpool = require('workerpool');-// Emit ready signal after startup-process.send({ type: 'ready' });+workerpool.worker({+ send,+});diff --git a/test/activitypub/sendPool.js b/test/activitypub/sendPool.jsindex 209721b69c..12c64e2784 100644--- a/test/activitypub/sendPool.js+++ b/test/activitypub/sendPool.js@@ -1,148 +1,80 @@'use strict';const assert = require('assert');-const os = require('os');const db = require('../mocks/databasemock');const activitypub = require('../../src/activitypub');const SendPool = activitypub.SendPool;describe('SendPool', () => {- /**- * Reset SendPool state to a clean initialization without forking workers.- * This allows tests to run in isolation without shared worker state.- */- function resetPool() {- // Kill all existing workers- for (const worker of SendPool.pool) {- try { worker.kill('SIGKILL'); } catch (e) { /* ignore */ }- }- SendPool.pool = [];- SendPool.free = [];- SendPool.busy = new Map();- SendPool.pending = new Map();- SendPool.taskTimers = new Map();- SendPool.inFlight = new Set();- SendPool.draining = false;- SendPool.isShuttingDown = false;- SendPool.maxWorkers = Math.max(1, os.cpus().length - 1);- }+ let originalActivityPub;+ let originalPoolSize;beforeEach(() => {- resetPool();+ originalActivityPub = SendPool._activityPub;+ originalPoolSize = SendPool._pool?.size;});afterEach((done) => {- // Shutdown any remaining workers- SendPool.isShuttingDown = true;- SendPool.draining = false;- for (const worker of SendPool.pool) {- try { worker.kill('SIGKILL'); } catch (e) { /* ignore */ }- }- // Clear any pending timers- for (const timer of SendPool.taskTimers.values()) {- clearTimeout(timer);- }- SendPool.taskTimers.clear();- // Wait a tick for workers to exit- setTimeout(done, 200);- });+ // Restore original state+ SendPool._activityPub = originalActivityPub;+ SendPool._draining = false;- describe('Pool lifecycle', () => {- it('should have empty pool after reset', () => {- assert(Array.isArray(SendPool.pool));- assert.strictEqual(SendPool.pool.length, 0);- });-- it('should have empty free list after reset', () => {- assert(Array.isArray(SendPool.free));- assert.strictEqual(SendPool.free.length, 0);- });-- it('should have empty busy map after reset', () => {- assert(SendPool.busy instanceof Map);- assert.strictEqual(SendPool.busy.size, 0);- });-- it('should have empty pending map after reset', () => {- assert(SendPool.pending instanceof Map);- assert.strictEqual(SendPool.pending.size, 0);- });+ // Shutdown pool+ SendPool.shutdown();- it('should have empty inFlight set after reset', () => {- assert(SendPool.inFlight instanceof Set);- assert.strictEqual(SendPool.inFlight.size, 0);- });-- it('should have draining set to false after reset', () => {- assert.strictEqual(SendPool.draining, false);- });+ // Wait for workers to exit+ setTimeout(done, 1000);+ });- it('should have isShuttingDown set to false after reset', () => {- assert.strictEqual(SendPool.isShuttingDown, false);+ describe('Pool lifecycle', () => {+ it('should create a pool with worker count property', () => {+ assert(Number.isInteger(SendPool.pool));});- it('should have maxWorkers set to cpus() - 1', () => {- assert.strictEqual(SendPool.maxWorkers, Math.max(1, os.cpus().length - 1));+ it('should return the current worker count', () => {+ // Pool might not have workers initialized yet (lazy startup)+ // so we just verify it's a non-negative integer+ assert(SendPool.pool >= 0);});});describe('dispatch()', () => {- it('should return false when no free workers are available', () => {- const result = SendPool.dispatch('test-task-1', {- queueId: 'test-queue-1',- uri: 'https://example.org/inbox',- id: 'test-id',- payloadType: 'Create',- payload: JSON.stringify({ type: 'Create' }),- digest: 'SHA-256=test',- });- assert.strictEqual(result, false);- });+ it('should return a thenable when pool has workers', async () => {+ // Wait briefly for workerpool to initialize workers+ await new Promise(resolve => setTimeout(resolve, 500));- it('should not add tasks to pending when dispatch fails', () => {- SendPool.dispatch('test-task-2', {- queueId: 'test-queue-2',- uri: 'https://example.org/inbox',+ const result = SendPool.dispatch({+ queueId: 'test-queue-1',+ uri: 'https://nonexistent.invalid/test',id: 'test-id',payloadType: 'Create',payload: JSON.stringify({ type: 'Create' }),- digest: 'SHA-256=test',+ digest: 'SHA-256=invalid',});- assert(!SendPool.pending.has('test-task-2'));+ assert(typeof result?.then === 'function');+ // Wait for the promise to settle+ const resolved = await result;+ assert.strictEqual(resolved.success, false);});});describe('handleResult()', () => {- it('should not fail when handling a result for a non-existent task', () => {+ it('should not fail when handling a result for a non-existent queue', () => {assert.doesNotThrow(() => {- SendPool.handleResult({- id: 'non-existent-task',+ SendPool.handleResult('non-existent-queue', {success: true,});});});- it('should clear a task timer if the task exists in pending', () => {- // Pre-populate pending with a task- const timer = setTimeout(() => {}, 60000);- SendPool.taskTimers.set('existing-task', timer);- SendPool.pending.set('existing-task', {- queueId: 'test-queue-existing',- uri: 'https://example.org/inbox',- id: 'test-id',- payloadType: 'Create',- payload: JSON.stringify({ type: 'Create' }),- digest: 'SHA-256=test',- });-- SendPool.handleResult({- id: 'existing-task',- success: true,+ it('should handle failure result without error', () => {+ assert.doesNotThrow(() => {+ SendPool.handleResult('fail-queue', {+ success: false,+ error: 'test error',+ });});-- assert(!SendPool.pending.has('existing-task'));- assert(!SendPool.taskTimers.has('existing-task'));});});@@ -216,98 +148,20 @@ describe('SendPool', () => {});});- describe('clearTaskTimer()', () => {- it('should clear a task timer', () => {- const timer = setTimeout(() => {}, 60000);- SendPool.taskTimers.set('timer-test-1', timer);- SendPool.clearTaskTimer('timer-test-1');- assert(!SendPool.taskTimers.has('timer-test-1'));- });-- it('should not fail when clearing a non-existent timer', () => {- assert.doesNotThrow(() => {- SendPool.clearTaskTimer('non-existent-timer');- });- });- });-describe('shutdown()', () => {- it('should set isShuttingDown to true', () => {- SendPool.shutdown();- assert.strictEqual(SendPool.isShuttingDown, true);- });-it('should set draining to false', () => {+ SendPool._draining = true;SendPool.shutdown();- assert.strictEqual(SendPool.draining, false);+ assert.strictEqual(SendPool._draining, false);});});describe('drainLoop()', () => {it('should not start a new drain loop if already draining', async () => {- SendPool.draining = true;+ SendPool._draining = true;await SendPool.drainLoop();// Should return immediately without starting a new loop- assert.strictEqual(SendPool.draining, true);- });- });-- describe('forkWorker()', () => {- it('should add a new worker to the pool', () => {- const poolLengthBefore = SendPool.pool.length;- SendPool.forkWorker();- assert.strictEqual(SendPool.pool.length, poolLengthBefore + 1);- });-- it('should add the worker to the free list after ready', (done) => {- const freeLengthBefore = SendPool.free.length;- SendPool.forkWorker();-- // The worker should be added to the free list after receiving the ready message- setTimeout(() => {- assert(SendPool.free.length >= freeLengthBefore);- done();- }, 500);- });- });-- describe('handleWorkerExit()', () => {- it('should not fork a replacement worker when shutting down', () => {- SendPool.isShuttingDown = true;- // Simulate a worker exit- const mockWorker = { kill: () => {} };- SendPool.busy.set(mockWorker, 'test-task');- SendPool.pending.set('test-task', {- queueId: 'test-queue',- uri: 'https://example.org/inbox',- id: 'test-id',- payloadType: 'Create',- payload: JSON.stringify({ type: 'Create' }),- digest: 'SHA-256=test',- });- SendPool.inFlight.add('test-queue');-- const poolLengthBefore = SendPool.pool.length;- SendPool.handleWorkerExit(mockWorker, 1);-- // Should not have forked a replacement- assert.strictEqual(SendPool.pool.length, poolLengthBefore);- });-- it('should handle exit when worker had no task (busy map empty for that worker)', () => {- // This tests the bug fix where task could be undefined- const mockWorker = { kill: () => {} };- SendPool.pool.push(mockWorker);- SendPool.free.push(mockWorker);-- // Worker exits without having been assigned a task- assert.doesNotThrow(() => {- SendPool.handleWorkerExit(mockWorker, 0);- });-- // Worker should be removed from pool and free- assert(!SendPool.pool.includes(mockWorker));- assert(!SendPool.free.includes(mockWorker));+ assert.strictEqual(SendPool._draining, true);});});});diff --git a/test/activitypub/sendWorker.js b/test/activitypub/sendWorker.jsindex 38f0b784cd..fdabd87df9 100644--- a/test/activitypub/sendWorker.js+++ b/test/activitypub/sendWorker.js@@ -1,215 +1,96 @@'use strict';const assert = require('assert');-const { fork } = require('child_process');const path = require('path');+const workerpool = require('workerpool');describe('sendWorker', () => {- let worker;+ let pool;++ beforeEach(() => {+ pool = workerpool.pool(path.join(__dirname, '../../src/activitypub/sendWorker.js'), {+ minWorkers: 1,+ maxWorkers: 1,+ workerType: 'process',+ forkOpts: { silent: true },+ });+ });afterEach((done) => {- if (worker) {- // Worker may have already exited (e.g., from a shutdown test)- if (worker.killed || worker.exitCode !== null) {- worker = null;- return done();- }- worker.kill('SIGKILL');- worker.on('exit', () => {- worker = null;- done();- });- } else {+ pool.terminate(true, 1000).then(() => {+ done();+ }).catch(() => {done();- }- });-- describe('IPC protocol', () => {- it('should emit a ready message on startup', (done) => {- worker = fork(path.join(__dirname, '../../src/activitypub/sendWorker.js'), [], {- silent: true,- env: { AP_SEND_CHILD: 'true' },- });-- worker.on('message', (message) => {- assert.strictEqual(message.type, 'ready');- done();- });- });-- it('should emit a result message on failed send', (done) => {- worker = fork(path.join(__dirname, '../../src/activitypub/sendWorker.js'), [], {- silent: true,- env: { AP_SEND_CHILD: 'true' },- });-- let readyReceived = false;-- worker.on('message', (message) => {- if (message.type === 'ready') {- readyReceived = true;- // Send a task to a non-existent endpoint (will fail, but tests the protocol)- worker.send({- type: 'send',- id: 'test-task-1',- uri: 'https://nonexistent.invalid/test',- payload: JSON.stringify({ type: 'Create' }),- digest: 'SHA-256=invalid',- key: { privateKeyPem: 'dummy' },- keyId: 'https://example.org/actor#key',- });- } else if (message.type === 'result' && readyReceived) {- assert.strictEqual(message.id, 'test-task-1');- assert.strictEqual(message.success, false); // Will fail due to DNS resolution- done();- }- });});+ });- it('should handle shutdown message and exit gracefully', (done) => {- worker = fork(path.join(__dirname, '../../src/activitypub/sendWorker.js'), [], {- silent: true,- env: { AP_SEND_CHILD: 'true' },- });-- worker.on('message', (message) => {- if (message.type === 'ready') {- worker.send({ type: 'shutdown' });- }- });-- worker.on('exit', (code) => {- assert.strictEqual(code, 0); // Worker exits with code 0 on graceful shutdown- done();- });+ describe('send task', () => {+ it('should emit a result on failed send', async () => {+ const result = await pool.exec('send', [{+ id: 'test-task-1',+ uri: 'https://nonexistent.invalid/test',+ payload: JSON.stringify({ type: 'Create' }),+ digest: 'SHA-256=invalid',+ key: '-----BEGIN PRIVATE KEY-----\nINVALID\n-----END PRIVATE KEY-----',+ keyId: 'https://example.org/actor#key',+ }], { timeout: 10000 });+ assert.strictEqual(result.success, false);});- it('should ignore malformed messages without crashing', (done) => {- worker = fork(path.join(__dirname, '../../src/activitypub/sendWorker.js'), [], {- silent: true,- env: { AP_SEND_CHILD: 'true' },- });-- worker.on('message', (message) => {- if (message.type === 'ready') {- // Send malformed messages — worker should ignore them- worker.send(null);- worker.send({});- worker.send({ type: 'unknown-type' });- // After a short delay, send shutdown- setTimeout(() => {- worker.send({ type: 'shutdown' });- }, 100);- }- });-- worker.on('exit', (code) => {- // Worker should exit gracefully (code 0) after shutdown- assert.strictEqual(code, 0);- done();- });+ it('should reject tasks with missing fields', async () => {+ let result;+ try {+ result = await pool.exec('send', [{+ id: 'missing-fields-test',+ uri: 'https://example.org/test',+ // Missing payload, digest, key, keyId+ }], { timeout: 5000 });+ } catch (e) {+ result = { success: false, error: e.message };+ }+ assert.strictEqual(result.success, false);+ assert(result.error.toLowerCase().includes('missing'));});});describe('SSRF protection', () => {- it('should reject localhost URLs', (done) => {- worker = fork(path.join(__dirname, '../../src/activitypub/sendWorker.js'), [], {- silent: true,- env: { AP_SEND_CHILD: 'true' },- });-- worker.on('message', (message) => {- if (message.type === 'ready') {- worker.send({- type: 'send',- id: 'ssrf-test-1',- uri: 'https://127.0.0.1/test',- payload: JSON.stringify({ type: 'Create' }),- digest: 'SHA-256=invalid',- key: { privateKeyPem: 'dummy' },- keyId: 'https://example.org/actor#key',- });- } else if (message.type === 'result' && message.id === 'ssrf-test-1') {- assert.strictEqual(message.success, false);- assert(message.error.toLowerCase().includes('ssrf') || message.error.toLowerCase().includes('forbidden'));- done();- }- });+ it('should reject localhost URLs', async () => {+ const result = await pool.exec('send', [{+ id: 'ssrf-test-1',+ uri: 'https://127.0.0.1/test',+ payload: JSON.stringify({ type: 'Create' }),+ digest: 'SHA-256=invalid',+ key: '-----BEGIN PRIVATE KEY-----\nINVALID\n-----END PRIVATE KEY-----',+ keyId: 'https://example.org/actor#key',+ }], { timeout: 10000 });+ assert.strictEqual(result.success, false);+ assert(result.error.toLowerCase().includes('ssrf') || result.error.toLowerCase().includes('forbidden'));});- it('should reject private IP URLs', (done) => {- worker = fork(path.join(__dirname, '../../src/activitypub/sendWorker.js'), [], {- silent: true,- env: { AP_SEND_CHILD: 'true' },- });-- worker.on('message', (message) => {- if (message.type === 'ready') {- worker.send({- type: 'send',- id: 'ssrf-test-2',- uri: 'https://192.168.1.1/test',- payload: JSON.stringify({ type: 'Create' }),- digest: 'SHA-256=invalid',- key: { privateKeyPem: 'dummy' },- keyId: 'https://example.org/actor#key',- });- } else if (message.type === 'result' && message.id === 'ssrf-test-2') {- assert.strictEqual(message.success, false);- assert(message.error.toLowerCase().includes('ssrf') || message.error.toLowerCase().includes('forbidden'));- done();- }- });- });-- it('should reject link-local IP URLs', (done) => {- worker = fork(path.join(__dirname, '../../src/activitypub/sendWorker.js'), [], {- silent: true,- env: { AP_SEND_CHILD: 'true' },- });-- worker.on('message', (message) => {- if (message.type === 'ready') {- worker.send({- type: 'send',- id: 'ssrf-test-3',- uri: 'https://169.254.169.254/latest/meta-data/',- payload: JSON.stringify({ type: 'Create' }),- digest: 'SHA-256=invalid',- key: { privateKeyPem: 'dummy' },- keyId: 'https://example.org/actor#key',- });- } else if (message.type === 'result' && message.id === 'ssrf-test-3') {- assert.strictEqual(message.success, false);- assert(message.error.toLowerCase().includes('ssrf') || message.error.toLowerCase().includes('forbidden'));- done();- }- });+ it('should reject private IP URLs', async () => {+ const result = await pool.exec('send', [{+ id: 'ssrf-test-2',+ uri: 'https://192.168.1.1/test',+ payload: JSON.stringify({ type: 'Create' }),+ digest: 'SHA-256=invalid',+ key: '-----BEGIN PRIVATE KEY-----\nINVALID\n-----END PRIVATE KEY-----',+ keyId: 'https://example.org/actor#key',+ }], { timeout: 10000 });+ assert.strictEqual(result.success, false);+ assert(result.error.toLowerCase().includes('ssrf') || result.error.toLowerCase().includes('forbidden'));});- });-- describe('Missing fields validation', () => {- it('should reject tasks with missing fields', (done) => {- worker = fork(path.join(__dirname, '../../src/activitypub/sendWorker.js'), [], {- silent: true,- env: { AP_SEND_CHILD: 'true' },- });- worker.on('message', (message) => {- if (message.type === 'ready') {- // Send a task with missing fields- worker.send({- type: 'send',- id: 'missing-fields-test',- uri: 'https://example.org/test',- // Missing payload, digest, key, keyId- });- } else if (message.type === 'result' && message.id === 'missing-fields-test') {- assert.strictEqual(message.success, false);- assert(message.error.toLowerCase().includes('missing'));- done();- }- });+ it('should reject link-local IP URLs', async () => {+ const result = await pool.exec('send', [{+ id: 'ssrf-test-3',+ uri: 'https://169.254.169.254/latest/meta-data/',+ payload: JSON.stringify({ type: 'Create' }),+ digest: 'SHA-256=invalid',+ key: '-----BEGIN PRIVATE KEY-----\nINVALID\n-----END PRIVATE KEY-----',+ keyId: 'https://example.org/actor#key',+ }], { timeout: 10000 });+ assert.strictEqual(result.success, false);+ assert(result.error.toLowerCase().includes('ssrf') || result.error.toLowerCase().includes('forbidden'));});});});