refactor: allocate api::Session on cpp heap (#48141)

This commit is contained in:
Robo
2025-08-25 18:52:06 +09:00
committed by GitHub
parent 0917ed5f6f
commit 3ccb1bc0a8
32 changed files with 632 additions and 293 deletions

View File

@@ -27,19 +27,53 @@ describe('cpp heap', () => {
it('should record as node in heap snapshot', async () => {
const { remotely } = await startRemoteControlApp(['--expose-internals']);
const [nodeCount, hasPersistentParent] = await remotely(async (heap: string) => {
const result = await remotely(async (heap: string, snapshotHelper: string) => {
const { recordState } = require(heap);
const { containsRetainingPath } = require(snapshotHelper);
const state = recordState();
const rootNodes = state.snapshot.filter(
(node: any) => node.name === 'Electron / App' && node.type !== 'string');
const hasParent = rootNodes.some((node: any) => node.incomingEdges.some(
(edge: any) => {
return edge.type === 'element' && edge.from.name === 'C++ Persistent roots';
}));
return [rootNodes.length, hasParent];
}, path.join(__dirname, '../../third_party/electron_node/test/common/heap'));
expect(nodeCount).to.equal(1);
expect(hasPersistentParent).to.be.true();
return containsRetainingPath(state.snapshot, ['C++ Persistent roots', 'Electron / App']);
}, path.join(__dirname, '../../third_party/electron_node/test/common/heap'),
path.join(__dirname, 'lib', 'heapsnapshot-helpers.js'));
expect(result).to.equal(true);
});
});
describe('session module', () => {
it('should record as node in heap snapshot', async () => {
const { remotely } = await startRemoteControlApp(['--expose-internals']);
const result = await remotely(async (heap: string, snapshotHelper: string) => {
const { session, BrowserWindow } = require('electron');
const { once } = require('node:events');
const assert = require('node:assert');
const { recordState } = require(heap);
const { containsRetainingPath } = require(snapshotHelper);
const session1 = session.defaultSession;
console.log(session1.getStoragePath());
const session2 = session.fromPartition('cppheap1');
const session3 = session.fromPartition('cppheap1');
const session4 = session.fromPartition('cppheap2');
console.log(session2.cookies);
assert.strictEqual(session2, session3);
assert.notStrictEqual(session2, session4);
const w = new BrowserWindow({
show: false,
webPreferences: {
session: session.fromPartition('cppheap1')
}
});
await w.loadURL('about:blank');
const state = recordState();
const isClosed = once(w, 'closed');
w.destroy();
await isClosed;
const numSessions = containsRetainingPath(state.snapshot, ['C++ Persistent roots', 'Electron / Session'], {
occurrences: 4
});
const canTraceJSReferences = containsRetainingPath(state.snapshot, ['C++ Persistent roots', 'Electron / Session', 'Cookies']);
return numSessions && canTraceJSReferences;
}, path.join(__dirname, '../../third_party/electron_node/test/common/heap'),
path.join(__dirname, 'lib', 'heapsnapshot-helpers.js'));
expect(result).to.equal(true);
});
});
});

View File

@@ -0,0 +1,25 @@
export function containsRetainingPath (snapshot, retainingPath, options) {
let root = snapshot.filter(
(node) => node.name === retainingPath[0] && node.type !== 'string');
for (let i = 1; i < retainingPath.length; i++) {
const needle = retainingPath[i];
const newRoot = [];
for (const node of root) {
for (let j = 0; j < node.outgoingEdges.length; j++) {
const child = node.outgoingEdges[j].to;
if (child.type === 'string') continue;
if (child.name === needle) {
newRoot.push(child);
}
}
}
if (!newRoot.length) {
console.log(`No retaining path found for ${needle}`);
return false;
}
root = newRoot;
}
return options?.occurrances
? root.length === options.occurrances
: true;
}