@sentio/runtime 4.3.4-rc.1 → 4.3.5-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sentio/runtime",
3
- "version": "4.3.4-rc.1",
3
+ "version": "4.3.5-rc.1",
4
4
  "license": "Apache-2.0",
5
5
  "repository": {
6
6
  "type": "git",
package/src/db-context.ts CHANGED
@@ -61,6 +61,10 @@ export abstract class AbstractStoreContext implements IStoreContext {
61
61
  >()
62
62
  private statsInterval: NodeJS.Timeout | undefined
63
63
  private pendings: Promise<unknown>[] = []
64
+ // Set once the process has finished and the context was closed. Guards
65
+ // against a lingering batch timer emitting after the final result (which
66
+ // would both lose the write and desync the reused processor stream).
67
+ private closed = false
64
68
 
65
69
  constructor(readonly processId: number) {}
66
70
 
@@ -79,6 +83,12 @@ export abstract class AbstractStoreContext implements IStoreContext {
79
83
  return this.sendUpsertInBatch(request.value as DBRequest_DBUpsert)
80
84
  }
81
85
 
86
+ // Read-your-writes: any non-upsert op (get/list/delete) must observe the
87
+ // upserts issued before it. Flush the pending batch first so its dbRequest
88
+ // is sent ahead of this op on the same stream; the driver applies stream
89
+ // ops in arrival order, so the read/delete sees the buffered writes.
90
+ this.sendBatch()
91
+
82
92
  const requestType = request.case as RequestType
83
93
  const opId = StoreContext.opCounter++
84
94
  const promise = this.newPromise<DBResponse>(opId, requestType)
@@ -160,6 +170,16 @@ export abstract class AbstractStoreContext implements IStoreContext {
160
170
  }
161
171
 
162
172
  close() {
173
+ this.closed = true
174
+ // Drop any un-flushed batch and cancel its timer so it can never emit
175
+ // after close. In the normal path awaitPendings() has already flushed it;
176
+ // reaching here with a pending batch means the process ended without
177
+ // awaiting the write (e.g. a handler error), so emitting it now would be
178
+ // both too late (lost from this checkpoint) and stream-corrupting.
179
+ if (this.upsertBatch) {
180
+ clearTimeout(this.upsertBatch.timer)
181
+ this.upsertBatch = undefined
182
+ }
163
183
  for (const [opId, defer] of this.defers) {
164
184
  // console.warn('context closed before db response', opId)
165
185
  defer.reject(new Error('context closed before db response, processId: ' + this.processId + ' opId: ' + opId))
@@ -221,6 +241,11 @@ export abstract class AbstractStoreContext implements IStoreContext {
221
241
  }
222
242
 
223
243
  private sendBatch() {
244
+ // Never emit once the context is closed: the process already sent its
245
+ // final result and the stream may have been handed to another process.
246
+ if (this.closed) {
247
+ return
248
+ }
224
249
  if (this.upsertBatch) {
225
250
  const { request, opId, timer } = this.upsertBatch
226
251
  // console.debug('sending batch upsert', opId, 'batch size', request?.entity.length)
@@ -242,6 +267,17 @@ export abstract class AbstractStoreContext implements IStoreContext {
242
267
  }
243
268
 
244
269
  async awaitPendings() {
270
+ // Flush any buffered upsert batch and wait for its ack before returning.
271
+ // Callers use this to guarantee every write has been sent AND applied by
272
+ // the driver before the process emits its final result. Without it the
273
+ // batch's setTimeout could fire after the result — losing the write and
274
+ // leaving a stray message on the pooled processor stream (surfacing to the
275
+ // driver as ERR200 "unexpected ProcessID").
276
+ if (this.upsertBatch) {
277
+ const { promise } = this.upsertBatch
278
+ this.sendBatch()
279
+ this.pendings.push(promise)
280
+ }
245
281
  await Promise.all(this.pendings)
246
282
  }
247
283
  }
@@ -1 +0,0 @@
1
- {"version":3,"file":"plugin-DnkcGTkw.d.ts","names":[],"sources":["../src/db-context.ts","../src/plugin.ts"],"mappings":";;;;;;KA8BK,yBAAA,GAA4B,gBAAgB,QAAQ,2BAAA;AAAA,KACpD,2BAAA,GAA8B,gBAAgB,QAAQ,6BAAA;AAAA,KAItD,OAAA,GAAU,WAAA,CAAY,gBAAA,QAAwB,eAAA;AAAA,KAC9C,WAAA,GAAc,WAAW,CAAC,OAAA;AAAA,cAElB,YAAA,EAAY,KAAuB;AAAA,UAE/B,aAAA;EACf,WAAA,CAAY,OAAA,EAAS,OAAA,EAAS,WAAA,YAAuB,OAAA,CAAQ,UAAA;EAE7D,MAAA,CAAO,QAAA,EAAU,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