Skip to content

Commit 5a252b2

Browse files
ColinGPT9claude
andauthored
Ollama's log: open it without a look first, set it aside on quit (#167)
#166 read the log's size from the open file but then reopened it by name once it had been set aside, and code scanning counts an open as a look (js/file-system-race #301). Windows won't let a file opened for adding to be emptied, so the size check moves to the way out: stopChildren sets a log past 10 MB aside as ollama.old.log once Ollama is stopped, and the next start opens a new one with nothing looked at first. A run that ends in a crash is still in ollama.log after the restart, as before. Co-authored-by: ColinGPT9 <ColinGPT9@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 7a02c4b commit 5a252b2

1 file changed

Lines changed: 29 additions & 17 deletions

File tree

‎ui/src/main/index.ts‎

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ import { randomBytes } from 'node:crypto'
1616
import {
1717
closeSync,
1818
existsSync,
19-
fstatSync,
2019
mkdirSync,
2120
openSync,
2221
readFileSync,
2322
renameSync,
23+
statSync,
2424
writeFileSync,
2525
writeSync
2626
} from 'node:fs'
@@ -186,28 +186,24 @@ ipcMain.handle('tray:set', (_event, on: unknown) => {
186186
return { keepInTray }
187187
})
188188

189+
/** The folder Ollama's log is in, once startOllama has opened it there. */
190+
let ollamaLogDir: string | null = null
191+
189192
/** Open Ollama's log for adding to, or null when it can't be written: the
190193
* runtime then starts with its output thrown away, as it always used to.
191194
* Ollama writes to the file itself rather than through a pipe this process
192-
* reads, so a burst of output can never stall it or the app. */
195+
* reads, so a burst of output can never stall it or the app.
196+
*
197+
* Opened without looking at it first: its size is seen to on the way out
198+
* (setAsideBigOllamaLog). Looked at by its name and then opened by that name,
199+
* the name can come to mean another file in between, which is what code
200+
* scanning flags (js/file-system-race), and Windows won't let a file opened
201+
* for adding to be emptied instead. */
193202
function openOllamaLog(dir: string): number | null {
194203
try {
195204
mkdirSync(dir, { recursive: true })
196-
const file = join(dir, 'ollama.log')
197-
// Ollama logs every request it serves, so the file would grow for as long
198-
// as the app is installed. Past 10 MB it is set aside and a new one begun.
199-
// The size is read from the open file, not by its name before opening it:
200-
// between a look by name and the open, the name can point somewhere else.
201-
let fd = openSync(file, 'a')
202-
if (fstatSync(fd).size > 10 * 1024 * 1024) {
203-
closeSync(fd)
204-
try {
205-
renameSync(file, join(dir, 'ollama.old.log'))
206-
} catch {
207-
// Held open by an Ollama still running: it is added to instead.
208-
}
209-
fd = openSync(file, 'a')
210-
}
205+
const fd = openSync(join(dir, 'ollama.log'), 'a')
206+
ollamaLogDir = dir
211207
try {
212208
writeSync(fd, `\n--- ${new Date().toISOString()} Clips Kitty started Ollama ---\n`)
213209
} catch {
@@ -317,6 +313,22 @@ function stopChildren(): void {
317313
killTree(ollama)
318314
backend = null
319315
ollama = null
316+
setAsideBigOllamaLog()
317+
}
318+
319+
/** Ollama logs every request it serves, so its log would grow for as long as
320+
* the app is installed. Past 10 MB it is set aside as ollama.old.log, after
321+
* Ollama has been stopped, and the next start begins a new one. Until then
322+
* it is added to, so the run before a restart is still in ollama.log, where
323+
* anyone looking into a problem would look. */
324+
function setAsideBigOllamaLog(): void {
325+
if (ollamaLogDir === null) return
326+
const file = join(ollamaLogDir, 'ollama.log')
327+
try {
328+
if (statSync(file).size > 10 * 1024 * 1024) renameSync(file, join(ollamaLogDir, 'ollama.old.log'))
329+
} catch {
330+
// No log, or it can't be moved just now: the next quit tries again.
331+
}
320332
}
321333

322334
function startBackend(): void {

0 commit comments

Comments
 (0)