Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

fix(rpc): async error (#6602)

Univerf5afdfe06166
diff --git a/packages/rpc/src/services/rpc/rpc.service.ts b/packages/rpc/src/services/rpc/rpc.service.ts
index 3f9efe3a59..7059e31189 100644
--- a/packages/rpc/src/services/rpc/rpc.service.ts
+++ b/packages/rpc/src/services/rpc/rpc.service.ts
@@ -232,17 +232,24 @@ export class ChannelClient extends RxDisposable implements IChannelClient {
);
}
- private async _remoteCall(channelName: string, method: string, args?: any): Promise<any> {
- await this._whenReady();
+ private _remoteCall(channelName: string, method: string, args?: any): Promise<any> {
+ // Fast path: if the channel is already initialized, execute synchronously
+ // to avoid yielding to the microtask queue. This prevents race conditions
+ // where mutable args (e.g. snapshots) could be modified before serialization.
+ if (this._initialized.getValue()) {
+ return this._doRemoteCall(channelName, method, args);
+ }
+
+ return this._whenReady().then(() => this._doRemoteCall(channelName, method, args));
+ }
+ private _doRemoteCall(channelName: string, method: string, args?: any): Promise<any> {
const sequence = ++this._lastRequestCounter;
const type = RequestType.CALL;
const request: IRPCRequest = { seq: sequence, type, channelName, method, args };
const client = this;
return new Promise((resolve, reject) => {
- // We trigger remote calling in this Promise's callback and deal
- // with response here as well.
const responseHandler: IResponseHandler = {
handle(response: IRPCResponse) {
switch (response.type) {
@@ -268,7 +275,8 @@ export class ChannelClient extends RxDisposable implements IChannelClient {
private _remoteSubscribe(channelName: string, method: string, args?: any): Observable<any> {
return new Observable((subscriber) => {
let sequence: number = -1;
- this._whenReady().then(() => {
+
+ const doSubscribe = () => {
sequence = ++this._lastRequestCounter;
const type = RequestType.SUBSCRIBE;
const request: IRPCRequest = { seq: sequence, type, channelName, method, args };
@@ -293,7 +301,15 @@ export class ChannelClient extends RxDisposable implements IChannelClient {
this._pendingRequests.set(sequence, responseHandler);
this._sendRequest(request);
- });
+ };
+
+ // Fast path: if the channel is already initialized, execute synchronously
+ // to avoid yielding to the microtask queue.
+ if (this._initialized.getValue()) {
+ doSubscribe();
+ } else {
+ this._whenReady().then(doSubscribe);
+ }
return () => {
if (sequence === -1) {