Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(desktop): open sync bounded reads in non-blocking mode

Yuewei-LL6bd9b1a3686e
diff --git a/apps/desktop/src/main/utils/__tests__/readBoundedFile.test.ts b/apps/desktop/src/main/utils/__tests__/readBoundedFile.test.ts
index 42dd20ef2c..74c6b24e54 100644
--- a/apps/desktop/src/main/utils/__tests__/readBoundedFile.test.ts
+++ b/apps/desktop/src/main/utils/__tests__/readBoundedFile.test.ts
@@ -401,7 +401,7 @@ describe('readBoundedFileNoFollow', () => {
});
describe('readBoundedFileNoFollowSync', () => {
- it('opens untrusted no-follow paths in blocking mode by default (sync variant)', async () => {
+ it('opens untrusted no-follow paths in non-blocking mode (sync variant)', async () => {
const file = path.join(workDir, 'plain.json');
await fs.promises.writeFile(file, '{"ok":1}');
const realOpenSync = fs.openSync;
@@ -419,10 +419,10 @@ describe('readBoundedFileNoFollowSync', () => {
spy.mockRestore();
}
- // The synchronous variant has no nonBlocking option; the default no-follow
- // open stays blocking (regular files only, so this cannot block on a FIFO).
+ // The sync variant opens non-blocking so a FIFO/device entry cannot block
+ // Main forever; O_NONBLOCK is a no-op for regular files.
const nonBlockingFlag = fs.constants.O_NONBLOCK ?? 0;
- expect(flags & nonBlockingFlag).toBe(0);
+ expect(flags & nonBlockingFlag).toBe(nonBlockingFlag);
});
it('限量与回退闸语义和异步变体一致', async () => {
diff --git a/apps/desktop/src/main/utils/readBoundedFile.ts b/apps/desktop/src/main/utils/readBoundedFile.ts
index 4570e553ff..dda046d32a 100644
--- a/apps/desktop/src/main/utils/readBoundedFile.ts
+++ b/apps/desktop/src/main/utils/readBoundedFile.ts
@@ -315,7 +315,13 @@ export function readBoundedFileNoFollowSync(
): Buffer | null {
const noFollow =
options?.noFollowFlag !== undefined ? options.noFollowFlag : (fs.constants.O_NOFOLLOW ?? null);
- const fd = fs.openSync(filePath, fs.constants.O_RDONLY | (noFollow ?? 0));
+ // O_NONBLOCK:对普通文件是 no-op,但对 FIFO/设备在 open 时立即返回 EAGAIN 而不是
+ // 把 Main 永久阻塞 —— 同步读取路径(receipt / ledger / locale)只接受普通文件,
+ // 特殊文件必须 fail-closed 而不是挂起。
+ const fd = fs.openSync(
+ filePath,
+ fs.constants.O_RDONLY | (fs.constants.O_NONBLOCK ?? 0) | (noFollow ?? 0),
+ );
try {
const stat = fs.fstatSync(fd, { bigint: true });
if (!stat.isFile() || Number(stat.size) > maxBytes) return null;