fix: Postgres node with ssh tunnel getting into a broken state and not being recreated (#16054)

This commit is contained in:
Danny Martini
2025-06-13 14:38:21 +02:00
committed by GitHub
parent 80a784a50c
commit 879114b572
9 changed files with 606 additions and 199 deletions

View File

@@ -1,26 +1,31 @@
import { mock } from 'jest-mock-extended';
import { OperationalError, type Logger } from 'n8n-workflow';
import { ConnectionPoolManager } from '@utils/connection-pool-manager';
const ttl = 5 * 60 * 1000;
const cleanUpInterval = 60 * 1000;
const logger = mock<Logger>();
let cpm: ConnectionPoolManager;
beforeAll(() => {
jest.useFakeTimers();
cpm = ConnectionPoolManager.getInstance();
cpm = ConnectionPoolManager.getInstance(logger);
});
beforeEach(async () => {
await cpm.purgeConnections();
cpm.purgeConnections();
});
afterAll(() => {
cpm.onShutdown();
cpm.purgeConnections();
});
test('getInstance returns a singleton', () => {
const instance1 = ConnectionPoolManager.getInstance();
const instance2 = ConnectionPoolManager.getInstance();
const instance1 = ConnectionPoolManager.getInstance(logger);
const instance2 = ConnectionPoolManager.getInstance(logger);
expect(instance1).toBe(instance2);
});
@@ -29,25 +34,27 @@ describe('getConnection', () => {
test('calls fallBackHandler only once and returns the first value', async () => {
// ARRANGE
const connectionType = {};
const fallBackHandler = jest.fn().mockResolvedValue(connectionType);
const cleanUpHandler = jest.fn();
const fallBackHandler = jest.fn(async () => {
return connectionType;
});
const options = {
credentials: {},
nodeType: 'example',
nodeVersion: '1',
fallBackHandler,
cleanUpHandler,
wasUsed: jest.fn(),
};
// ACT 1
const connection = await cpm.getConnection<string>(options);
const connection = await cpm.getConnection(options);
// ASSERT 1
expect(fallBackHandler).toHaveBeenCalledTimes(1);
expect(connection).toBe(connectionType);
// ACT 2
const connection2 = await cpm.getConnection<string>(options);
const connection2 = await cpm.getConnection(options);
// ASSERT 2
expect(fallBackHandler).toHaveBeenCalledTimes(1);
expect(connection2).toBe(connectionType);
@@ -56,27 +63,29 @@ describe('getConnection', () => {
test('creates different pools for different node versions', async () => {
// ARRANGE
const connectionType1 = {};
const fallBackHandler1 = jest.fn().mockResolvedValue(connectionType1);
const cleanUpHandler1 = jest.fn();
const fallBackHandler1 = jest.fn(async () => {
return connectionType1;
});
const connectionType2 = {};
const fallBackHandler2 = jest.fn().mockResolvedValue(connectionType2);
const cleanUpHandler2 = jest.fn();
const fallBackHandler2 = jest.fn(async () => {
return connectionType2;
});
// ACT 1
const connection1 = await cpm.getConnection<string>({
const connection1 = await cpm.getConnection({
credentials: {},
nodeType: 'example',
nodeVersion: '1',
fallBackHandler: fallBackHandler1,
cleanUpHandler: cleanUpHandler1,
wasUsed: jest.fn(),
});
const connection2 = await cpm.getConnection<string>({
const connection2 = await cpm.getConnection({
credentials: {},
nodeType: 'example',
nodeVersion: '2',
fallBackHandler: fallBackHandler2,
cleanUpHandler: cleanUpHandler2,
wasUsed: jest.fn(),
});
// ASSERT
@@ -92,21 +101,52 @@ describe('getConnection', () => {
test('calls cleanUpHandler after TTL expires', async () => {
// ARRANGE
const connectionType = {};
const fallBackHandler = jest.fn().mockResolvedValue(connectionType);
const cleanUpHandler = jest.fn();
await cpm.getConnection<string>({
let abortController: AbortController | undefined;
const fallBackHandler = jest.fn(async (ac: AbortController) => {
abortController = ac;
return connectionType;
});
await cpm.getConnection({
credentials: {},
nodeType: 'example',
nodeVersion: '1',
fallBackHandler,
cleanUpHandler,
wasUsed: jest.fn(),
});
// ACT
jest.advanceTimersByTime(ttl + cleanUpInterval * 2);
// ASSERT
expect(cleanUpHandler).toHaveBeenCalledTimes(1);
if (abortController === undefined) {
fail("abortController haven't been initialized");
}
expect(abortController.signal.aborted).toBe(true);
});
test('throws OperationsError if the fallBackHandler aborts during connection initialization', async () => {
// ARRANGE
const connectionType = {};
const fallBackHandler = jest.fn(async (ac: AbortController) => {
ac.abort();
return connectionType;
});
// ACT
const connectionPromise = cpm.getConnection({
credentials: {},
nodeType: 'example',
nodeVersion: '1',
fallBackHandler,
wasUsed: jest.fn(),
});
// ASSERT
await expect(connectionPromise).rejects.toThrow(OperationalError);
await expect(connectionPromise).rejects.toThrow(
'Could not create pool. Connection attempt was aborted.',
);
});
});
@@ -114,66 +154,115 @@ describe('onShutdown', () => {
test('calls all clean up handlers', async () => {
// ARRANGE
const connectionType1 = {};
const fallBackHandler1 = jest.fn().mockResolvedValue(connectionType1);
const cleanUpHandler1 = jest.fn();
await cpm.getConnection<string>({
let abortController1: AbortController | undefined;
const fallBackHandler1 = jest.fn(async (ac: AbortController) => {
abortController1 = ac;
return connectionType1;
});
await cpm.getConnection({
credentials: {},
nodeType: 'example',
nodeVersion: '1',
fallBackHandler: fallBackHandler1,
cleanUpHandler: cleanUpHandler1,
wasUsed: jest.fn(),
});
const connectionType2 = {};
const fallBackHandler2 = jest.fn().mockResolvedValue(connectionType2);
const cleanUpHandler2 = jest.fn();
await cpm.getConnection<string>({
let abortController2: AbortController | undefined;
const fallBackHandler2 = jest.fn(async (ac: AbortController) => {
abortController2 = ac;
return connectionType2;
});
await cpm.getConnection({
credentials: {},
nodeType: 'example',
nodeVersion: '2',
fallBackHandler: fallBackHandler2,
cleanUpHandler: cleanUpHandler2,
wasUsed: jest.fn(),
});
// ACT 1
cpm.onShutdown();
// ACT
cpm.purgeConnections();
// ASSERT
expect(cleanUpHandler1).toHaveBeenCalledTimes(1);
expect(cleanUpHandler2).toHaveBeenCalledTimes(1);
if (abortController1 === undefined || abortController2 === undefined) {
fail("abortController haven't been initialized");
}
expect(abortController1.signal.aborted).toBe(true);
expect(abortController2.signal.aborted).toBe(true);
});
test('calls all clean up handlers when `exit` is emitted on process', async () => {
// ARRANGE
const connectionType1 = {};
const fallBackHandler1 = jest.fn().mockResolvedValue(connectionType1);
const cleanUpHandler1 = jest.fn();
await cpm.getConnection<string>({
let abortController1: AbortController | undefined;
const fallBackHandler1 = jest.fn(async (ac: AbortController) => {
abortController1 = ac;
return connectionType1;
});
await cpm.getConnection({
credentials: {},
nodeType: 'example',
nodeVersion: '1',
fallBackHandler: fallBackHandler1,
cleanUpHandler: cleanUpHandler1,
wasUsed: jest.fn(),
});
const connectionType2 = {};
const fallBackHandler2 = jest.fn().mockResolvedValue(connectionType2);
const cleanUpHandler2 = jest.fn();
await cpm.getConnection<string>({
let abortController2: AbortController | undefined;
const fallBackHandler2 = jest.fn(async (ac: AbortController) => {
abortController2 = ac;
return connectionType2;
});
await cpm.getConnection({
credentials: {},
nodeType: 'example',
nodeVersion: '2',
fallBackHandler: fallBackHandler2,
cleanUpHandler: cleanUpHandler2,
wasUsed: jest.fn(),
});
// ACT 1
// ACT
// @ts-expect-error we're not supposed to emit `exit` so it's missing from
// the type definition
process.emit('exit');
// ASSERT
expect(cleanUpHandler1).toHaveBeenCalledTimes(1);
expect(cleanUpHandler2).toHaveBeenCalledTimes(1);
if (abortController1 === undefined || abortController2 === undefined) {
fail("abortController haven't been initialized");
}
expect(abortController1.signal.aborted).toBe(true);
expect(abortController2.signal.aborted).toBe(true);
});
});
describe('wasUsed', () => {
test('is called for every successive `getConnection` call', async () => {
// ARRANGE
const connectionType = {};
const fallBackHandler = jest.fn(async () => {
return connectionType;
});
const wasUsed = jest.fn();
const options = {
credentials: {},
nodeType: 'example',
nodeVersion: '1',
fallBackHandler,
wasUsed,
};
// ACT 1
await cpm.getConnection(options);
// ASSERT 1
expect(wasUsed).toHaveBeenCalledTimes(0);
// ACT 2
await cpm.getConnection(options);
// ASSERT 2
expect(wasUsed).toHaveBeenCalledTimes(1);
});
});