Skip to content

Commit

Permalink
Ensure systrace events are always stopped (#39561)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #39561

Trying to investigate an issue where Systrace events aren't properly ended. For correctness, we should make sure to always end markers that we started.

Reviewed By: sammy-SC

Differential Revision: D49413689

fbshipit-source-id: e48e3aef20602e0af9deb924244cdfaf614d11f2
  • Loading branch information
javache authored and facebook-github-bot committed Sep 20, 2023
1 parent 630cf3b commit 97b6829
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 38 deletions.
81 changes: 45 additions & 36 deletions packages/react-native/Libraries/BatchedBridge/MessageQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,10 +388,13 @@ class MessageQueue {

__callReactNativeMicrotasks() {
Systrace.beginEvent('JSTimers.callReactNativeMicrotasks()');
if (this._reactNativeMicrotasksCallback != null) {
this._reactNativeMicrotasksCallback();
try {
if (this._reactNativeMicrotasksCallback != null) {
this._reactNativeMicrotasksCallback();
}
} finally {
Systrace.endEvent();
}
Systrace.endEvent();
}

__callFunction(module: string, method: string, args: mixed[]): void {
Expand All @@ -402,31 +405,35 @@ class MessageQueue {
} else {
Systrace.beginEvent(`${module}.${method}(...)`);
}
if (this.__spy) {
this.__spy({type: TO_JS, module, method, args});
}
const moduleMethods = this.getCallableModule(module);
if (!moduleMethods) {
const callableModuleNames = Object.keys(this._lazyCallableModules);
const n = callableModuleNames.length;
const callableModuleNameList = callableModuleNames.join(', ');

// TODO(T122225939): Remove after investigation: Why are we getting to this line in bridgeless mode?
const isBridgelessMode = global.RN$Bridgeless === true ? 'true' : 'false';
invariant(
false,
`Failed to call into JavaScript module method ${module}.${method}(). Module has not been registered as callable. Bridgeless Mode: ${isBridgelessMode}. Registered callable JavaScript modules (n = ${n}): ${callableModuleNameList}.
A frequent cause of the error is that the application entry file path is incorrect. This can also happen when the JS bundle is corrupt or there is an early initialization error when loading React Native.`,
);
}
if (!moduleMethods[method]) {
invariant(
false,
`Failed to call into JavaScript module method ${module}.${method}(). Module exists, but the method is undefined.`,
);
try {
if (this.__spy) {
this.__spy({type: TO_JS, module, method, args});
}
const moduleMethods = this.getCallableModule(module);
if (!moduleMethods) {
const callableModuleNames = Object.keys(this._lazyCallableModules);
const n = callableModuleNames.length;
const callableModuleNameList = callableModuleNames.join(', ');

// TODO(T122225939): Remove after investigation: Why are we getting to this line in bridgeless mode?
const isBridgelessMode =
global.RN$Bridgeless === true ? 'true' : 'false';
invariant(
false,
`Failed to call into JavaScript module method ${module}.${method}(). Module has not been registered as callable. Bridgeless Mode: ${isBridgelessMode}. Registered callable JavaScript modules (n = ${n}): ${callableModuleNameList}.
A frequent cause of the error is that the application entry file path is incorrect. This can also happen when the JS bundle is corrupt or there is an early initialization error when loading React Native.`,
);
}
if (!moduleMethods[method]) {
invariant(
false,
`Failed to call into JavaScript module method ${module}.${method}(). Module exists, but the method is undefined.`,
);
}
moduleMethods[method].apply(moduleMethods, args);
} finally {
Systrace.endEvent();
}
moduleMethods[method].apply(moduleMethods, args);
Systrace.endEvent();
}

__invokeCallback(cbID: number, args: mixed[]): void {
Expand Down Expand Up @@ -465,16 +472,18 @@ class MessageQueue {
);
}

if (!callback) {
return;
}

this._successCallbacks.delete(callID);
this._failureCallbacks.delete(callID);
callback(...args);
try {
if (!callback) {
return;
}

if (__DEV__) {
Systrace.endEvent();
this._successCallbacks.delete(callID);
this._failureCallbacks.delete(callID);
callback(...args);
} finally {
if (__DEV__) {
Systrace.endEvent();
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ describe('MessageQueue', function () {
const unknownModule = 'UnknownModule',
unknownMethod = 'UnknownMethod';
expect(() => queue.__callFunction(unknownModule, unknownMethod)).toThrow(
`Failed to call into JavaScript module method ${unknownModule}.${unknownMethod}(). Module has not been registered as callable. Bridgeless Mode: false. Registered callable JavaScript modules (n = 1): MessageQueueTestModule.
A frequent cause of the error is that the application entry file path is incorrect. This can also happen when the JS bundle is corrupt or there is an early initialization error when loading React Native.`,
`Failed to call into JavaScript module method ${unknownModule}.${unknownMethod}()`,
);
});

Expand Down

0 comments on commit 97b6829

Please sign in to comment.