Skip to content

Commit b0e8bd6

Browse files
committed
🐛 Unhandled rejection when submitting ops during hard rollback.
After doing the steps: 1. Create a doc with rich text type (or any other non irreversible type) 2. Make op submission fail 3. Now in the hard rollback we do `this._setType(null);` 4. If there is any op comming before the hard rollback `fetch` is finished, we get the error ``` Cannot submit op. Document has not been created. ``` as in the `_submit` we do: ```typescript if (!this.type) { var err = new ShareDBError( ERROR_CODE.ERR_DOC_DOES_NOT_EXIST, 'Cannot submit op. Document has not been created. ' + this.collection + '.' + this.id ); if (callback) return callback(err); return this.emit('error', err); } ``` We definitely do not handle this case properly. Possible solutions: 1. Just throw error whenever that happens, which is easy to implement and it is not really breaking. User would be then able to react on the error or just ignore it. 2. Store copy of cannonical snapshot in the doc itself, so that we do not have to do fetch for hard rollback. More difficult to implement and has a side effect of storing the doc twice in the memory.
1 parent ca50594 commit b0e8bd6

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

lib/client/doc.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ function Doc(connection, collection, id) {
7070
this.pendingFetch = [];
7171
this.pendingSubscribe = [];
7272

73+
this._isInHardRollback = false;
74+
7375
// Whether we think we are subscribed on the server. Synchronously set to
7476
// false on calls to unsubscribe and disconnect. Should never be true when
7577
// this.wantSubscribe is false
@@ -745,6 +747,15 @@ Doc.prototype._submit = function(op, source, callback) {
745747
// Locally submitted ops must always have a truthy source
746748
if (!source) source = true;
747749

750+
if (this._isInHardRollback) {
751+
var err = new ShareDBError(
752+
ERROR_CODE.ERR_DOC_IN_HARD_ROLLBACK,
753+
'Cannot submit op. Document is performing hard rollback. ' + this.collection + '.' + this.id
754+
);
755+
if (callback) return callback(err);
756+
return this.emit('error', err);
757+
}
758+
748759
// The op contains either op, create, delete, or none of the above (a no-op).
749760
if ('op' in op) {
750761
if (!this.type) {
@@ -1030,6 +1041,7 @@ Doc.prototype._rollback = function(err) {
10301041
};
10311042

10321043
Doc.prototype._hardRollback = function(err) {
1044+
this._isInHardRollback = true;
10331045
// Store pending ops so that we can notify their callbacks of the error.
10341046
// We combine the inflight op and the pending ops, because it's possible
10351047
// to hit a condition where we have no inflight op, but we do have pending
@@ -1077,7 +1089,10 @@ Doc.prototype._hardRollback = function(err) {
10771089
inflightOp = null;
10781090
}
10791091

1080-
if (!pendingOps.length) return;
1092+
if (!pendingOps.length) {
1093+
doc._isInHardRollback = false;
1094+
return;
1095+
}
10811096
err = new ShareDBError(
10821097
ERROR_CODE.ERR_PENDING_OP_REMOVED_BY_OP_SUBMIT_REJECTED,
10831098
'Discarding pending op because of hard rollback during ERR_OP_SUBMIT_REJECTED'
@@ -1090,6 +1105,7 @@ Doc.prototype._hardRollback = function(err) {
10901105
allOpsHadCallbacks = util.callEach(pendingOps[i].callbacks, err) && allOpsHadCallbacks;
10911106
}
10921107
if (err && !allOpsHadCallbacks) doc.emit('error', err);
1108+
doc._isInHardRollback = false;
10931109
});
10941110
};
10951111

lib/error.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ ShareDBError.CODES = {
3030
ERR_DOC_DOES_NOT_EXIST: 'ERR_DOC_DOES_NOT_EXIST',
3131
ERR_DOC_TYPE_NOT_RECOGNIZED: 'ERR_DOC_TYPE_NOT_RECOGNIZED',
3232
ERR_DOC_WAS_DELETED: 'ERR_DOC_WAS_DELETED',
33+
ERR_DOC_IN_HARD_ROLLBACK: 'ERR_DOC_IN_HARD_ROLLBACK',
3334
ERR_INFLIGHT_OP_MISSING: 'ERR_INFLIGHT_OP_MISSING',
3435
ERR_INGESTED_SNAPSHOT_HAS_NO_VERSION: 'ERR_INGESTED_SNAPSHOT_HAS_NO_VERSION',
3536
ERR_MAX_SUBMIT_RETRIES_EXCEEDED: 'ERR_MAX_SUBMIT_RETRIES_EXCEEDED',

0 commit comments

Comments
 (0)