Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/cmap/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,15 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
}
}

if (this.socket.write(buffer)) return;
try {
if (this.socket.write(buffer)) return;
} catch (writeError) {
const networkError = new MongoNetworkError('unexpected error writing to socket', {
cause: writeError
});
this.onError(networkError);
throw networkError;
}

const drainEvent = once<void>(this.socket, 'drain', options);
const timeout = options?.timeoutContext?.timeoutForSocketWrite;
Expand Down
65 changes: 63 additions & 2 deletions test/integration/node-specific/convert_socket_errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Duplex } from 'node:stream';
import { expect } from 'chai';
import * as sinon from 'sinon';

import { type MongoClient, MongoNetworkError } from '../../../src';
import { type Collection, type Document, type MongoClient, MongoNetworkError } from '../../../src';
import { Connection } from '../../../src/cmap/connection';
import { ns } from '../../../src/utils';
import { clearFailPoint, configureFailPoint } from '../../tools/utils';
Expand Down Expand Up @@ -41,7 +41,7 @@ describe('Socket Errors', () => {

describe('when destroyed by failpoint', () => {
let client: MongoClient;
let collection;
let collection: Collection<Document>;

const metadata: MongoDBMetadataUI = { requires: { mongodb: '>=4.4' } };

Expand Down Expand Up @@ -77,4 +77,65 @@ describe('Socket Errors', () => {
expect(error, error.stack).to.be.instanceOf(MongoNetworkError);
});
});

describe('when an error is thrown writing data to a socket', () => {
let client: MongoClient;
let collection: Collection<Document>;
let errorCount = 0;

beforeEach(async function () {
client = this.configuration.newClient({ monitorCommands: true });
await client.connect();
const db = client.db('closeConn');
collection = db.collection('closeConn');
const docs = Array.from({ length: 128 }).map((_, index) => ({ foo: index, bar: 1 }));
await collection.deleteMany({});
await collection.insertMany(docs);

for (const [, server] of client.topology.s.servers) {
//@ts-expect-error: private property
for (const connection of server.pool.connections) {
//@ts-expect-error: private property
const socket = connection.socket;
const stub = sinon.stub(socket, 'write').callsFake(function () {
errorCount++;
stub.restore();
throw new Error('This socket has been ended by the other party');
});
}
}
});

afterEach(async function () {
sinon.restore();
await client.close();
});

it('retries and succeeds', async () => {
const initialErrorCount = errorCount;
const commandSucceededEvents: string[] = [];
const commandFailedEvents: string[] = [];
const commandStartedEvents: string[] = [];

client.on('commandStarted', event => {
if (event.commandName === 'find') commandStartedEvents.push(event.commandName);
});
client.on('commandSucceeded', event => {
if (event.commandName === 'find') commandSucceededEvents.push(event.commandName);
});
client.on('commandFailed', event => {
if (event.commandName === 'find') commandFailedEvents.push(event.commandName);
});

// call find, fail once, succeed on retry
const item = await collection.findOne({});
// check that an object was returned
expect(item).to.exist;
expect(errorCount).to.be.equal(initialErrorCount + 1);
// check that we have the expected command monitoring events
expect(commandStartedEvents).to.have.length(2);
expect(commandFailedEvents).to.have.length(1);
expect(commandSucceededEvents).to.have.length(1);
});
});
});