Open pmogollons opened 4 weeks ago
Hi, thanks for opening this. Would you mind sharing a quick code example so I can understand the situation better? Also, can you explain the use case in more detail and the behavior you were expecting?
Hi Jam, sure.
I have collection hooks package setup to after an update of a doc in a certain collection get user data from DB and send an SMS message. Also in a method I have a transaction that updates a doc in that collection and triggers the after update hook.
On after hooks transaction is already commited, so any db op will fail which is using the session of the commited transaction.
Ops in hooks or at least in after hooks should not run in the transaction.
Deals.after.update(async function dealsAfterUpdate(userId, doc) {
if (doc.status === "WON" && this.previous.status !== "WON") {
// First DB op sometimes works as the transactions is not committed yet
const users = await Meteor.users.find({}).fetchAsync();
// Here is where it fails
const contact = await Contacts.findOneAsync({});
sendSMSMessage();
}
});
W20240419-18:31:58.145(-5)? (STDERR) (node:112055) UnhandledPromiseRejectionWarning: MongoServerError: Transaction with { txnNumber: 1 } has been committed.
W20240419-18:31:58.145(-5)? (STDERR) at Connection.onMessage (/home/wanchope/.meteor/packages/npm-mongo/.4.17.2.hhl6u3.xi4e8++os+web.browser+web.browser.legacy+web.cordova/npm/node_modules/mongodb/lib/cmap/connection.js:231:30)
W20240419-18:31:58.145(-5)? (STDERR) at MessageStream.<anonymous> (/home/wanchope/.meteor/packages/npm-mongo/.4.17.2.hhl6u3.xi4e8++os+web.browser+web.browser.legacy+web.cordova/npm/node_modules/mongodb/lib/cmap/connection.js:61:60)
W20240419-18:31:58.145(-5)? (STDERR) at MessageStream.emit (events.js:400:28)
W20240419-18:31:58.145(-5)? (STDERR) at MessageStream.emit (domain.js:475:12)
W20240419-18:31:58.145(-5)? (STDERR) at processIncomingData (/home/wanchope/.meteor/packages/npm-mongo/.4.17.2.hhl6u3.xi4e8++os+web.browser+web.browser.legacy+web.cordova/npm/node_modules/mongodb/lib/cmap/message_stream.js:125:16)
W20240419-18:31:58.145(-5)? (STDERR) at MessageStream._write (/home/wanchope/.meteor/packages/npm-mongo/.4.17.2.hhl6u3.xi4e8++os+web.browser+web.browser.legacy+web.cordova/npm/node_modules/mongodb/lib/cmap/message_stream.js:33:9)
W20240419-18:31:58.145(-5)? (STDERR) at writeOrBuffer (internal/streams/writable.js:358:12)
W20240419-18:31:58.146(-5)? (STDERR) at MessageStream.Writable.write (internal/streams/writable.js:303:10)
W20240419-18:31:58.146(-5)? (STDERR) at Socket.ondata (internal/streams/readable.js:731:22)
W20240419-18:31:58.146(-5)? (STDERR) at Socket.emit (events.js:400:28)
W20240419-18:31:58.146(-5)? (STDERR) at Socket.emit (domain.js:475:12)
W20240419-18:31:58.146(-5)? (STDERR) at addChunk (internal/streams/readable.js:293:12)
W20240419-18:31:58.146(-5)? (STDERR) at readableAddChunk (internal/streams/readable.js:267:9)
W20240419-18:31:58.146(-5)? (STDERR) at Socket.Readable.push (internal/streams/readable.js:206:10)
W20240419-18:31:58.146(-5)? (STDERR) at TCP.onStreamRead (internal/stream_base_commons.js:188:23)
W20240419-18:31:58.146(-5)? (STDERR) => awaited here:
W20240419-18:31:58.146(-5)? (STDERR) at Function.Promise.await (/home/wanchope/.meteor/packages/promise/.0.12.2.1cqaxce.vzzz++os+web.browser+web.browser.legacy+web.cordova/npm/node_modules/meteor-promise/promise_server.js:56:12)
W20240419-18:31:58.146(-5)? (STDERR) at packages/mongo/mongo_driver.js:1138:14
W20240419-18:31:58.146(-5)? (STDERR) at /home/wanchope/.meteor/packages/promise/.0.12.2.1cqaxce.vzzz++os+web.browser+web.browser.legacy+web.cordova/npm/node_modules/meteor-promise/fiber_pool.js:43:40
W20240419-18:31:58.146(-5)? (STDERR) => awaited here:
W20240419-18:31:58.146(-5)? (STDERR) at Promise.await (/home/wanchope/.meteor/packages/promise/.0.12.2.1cqaxce.vzzz++os+web.browser+web.browser.legacy+web.cordova/npm/node_modules/meteor-promise/promise_server.js:60:12)
W20240419-18:31:58.146(-5)? (STDERR) at SynchronousCursor._nextObject (packages/mongo/mongo_driver.js:1187:38)
W20240419-18:31:58.147(-5)? (STDERR) at SynchronousCursor.SyncronousCursor._nextObject (packages/montiapm:agent/lib/hijack/db.js:263:33)
W20240419-18:31:58.147(-5)? (STDERR) at SynchronousCursor.forEach (packages/mongo/mongo_driver.js:1202:22)
W20240419-18:31:58.147(-5)? (STDERR) at SynchronousCursor.map (packages/mongo/mongo_driver.js:1213:10)
W20240419-18:31:58.147(-5)? (STDERR) at SynchronousCursor.fetch (packages/mongo/mongo_driver.js:1237:17)
W20240419-18:31:58.147(-5)? (STDERR) at Cursor.<computed> (packages/mongo/mongo_driver.js:965:32)
W20240419-18:31:58.147(-5)? (STDERR) at Cursor.cursorProto.<computed> (packages/montiapm:agent/lib/hijack/db.js:168:32)
W20240419-18:31:58.147(-5)? (STDERR) at Cursor.kadira_Cursor_fetch [as fetch] (packages/montiapm:agent/lib/hijack/set_labels.js:72:32)
W20240419-18:31:58.147(-5)? (STDERR) at Cursor.<computed> [as fetchAsync] (packages/mongo/mongo_driver.js:978:46)
W20240419-18:31:58.147(-5)? (STDERR) at imports/api/deals/server/hooks.js:35:94
W20240419-18:31:58.147(-5)? (STDERR) at /home/wanchope/.meteor/packages/promise/.0.12.2.1cqaxce.vzzz++os+web.browser+web.browser.legacy+web.cordova/npm/node_modules/meteor-promise/fiber_pool.js:43:40
W20240419-18:31:58.147(-5)? (STDERR) => awaited here:
W20240419-18:31:58.147(-5)? (STDERR) at Function.Promise.await (/home/wanchope/.meteor/packages/promise/.0.12.2.1cqaxce.vzzz++os+web.browser+web.browser.legacy+web.cordova/npm/node_modules/meteor-promise/promise_server.js:56:12)
W20240419-18:31:58.147(-5)? (STDERR) at imports/api/deals/server/hooks.js:35:16
W20240419-18:31:58.147(-5)? (STDERR) at /home/wanchope/.meteor/packages/promise/.0.12.2.1cqaxce.vzzz++os+web.browser+web.browser.legacy+web.cordova/npm/node_modules/meteor-promise/fiber_pool.js:43:40
Allow to add an option to the DB op to opt-out of adding the session
const contact = await Contacts.findOneAsync({}, { disableTransactions: true });
When using this package with collection-hooks, the db ops inside a hook are within the transaction, but any transaction inside an after hook throws an error because the transaction has already being commited.