From c31fb2475205ba1b5cadd80ccf19dd8429b7ec6a Mon Sep 17 00:00:00 2001 From: Leo Vasanko Date: Sun, 12 Nov 2023 20:30:35 +0000 Subject: [PATCH] Correct locking in thread poll watcher --- cista/watching.py | 10 +++++----- frontend/src/repositories/WS.ts | 16 ++++++++-------- frontend/src/stores/documents.ts | 1 - 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/cista/watching.py b/cista/watching.py index 49c4f29..ab62ab6 100644 --- a/cista/watching.py +++ b/cista/watching.py @@ -1,7 +1,6 @@ import asyncio import shutil import stat -import sys import threading import time from os import stat_result @@ -190,10 +189,10 @@ def watcher_thread_poll(loop): while not quit: rootpath = config.config.path - old = state.root new = walk() - if old != new: - with state.lock: + with state.lock: + old = state.root + if old != new: state.root = new broadcast(format_update(old, new), loop) @@ -333,8 +332,9 @@ async def abroadcast(msg): async def start(app, loop): config.load_config() + use_inotify = False # sys.platform == "linux" app.ctx.watcher = threading.Thread( - target=watcher_thread if sys.platform == "linux" else watcher_thread_poll, + target=watcher_thread if use_inotify else watcher_thread_poll, args=[loop], ) app.ctx.watcher.start() diff --git a/frontend/src/repositories/WS.ts b/frontend/src/repositories/WS.ts index 0570921..a2476ce 100644 --- a/frontend/src/repositories/WS.ts +++ b/frontend/src/repositories/WS.ts @@ -126,16 +126,16 @@ function handleUpdateMessage(updateData: { update: UpdateEntry[] }) { let oidx = 0 for (const [action, arg] of update) { - if (action === 'k') { - newtree.push(...tree.slice(oidx, oidx + arg)) - oidx += arg - } - else if (action === 'd') oidx += arg - else if (action === 'i') newtree.push(...arg) - else console.log("Unknown update action", action, arg) + if (action === 'k') { + newtree.push(...tree.slice(oidx, oidx + arg)) + oidx += arg + } + else if (action === 'd') oidx += arg + else if (action === 'i') newtree.push(...arg) + else console.log("Unknown update action", action, arg) } if (oidx != tree.length) - throw Error(`Tree update out of sync, number of entries mismatch: got ${oidx}, expected ${tree.length}`) + throw Error(`Tree update out of sync, number of entries mismatch: got ${oidx}, expected ${tree.length}, new tree ${newtree.length}`) store.updateRoot(newtree) tree = newtree saveSession() diff --git a/frontend/src/stores/documents.ts b/frontend/src/stores/documents.ts index 0d40d75..6004d19 100644 --- a/frontend/src/stores/documents.ts +++ b/frontend/src/stores/documents.ts @@ -4,7 +4,6 @@ import { defineStore } from 'pinia' import { collator } from '@/utils' import { logoutUser } from '@/repositories/User' import { watchConnect } from '@/repositories/WS' -import { format } from 'path' type FileData = { id: string; mtime: number; size: number; dir: DirectoryData } type DirectoryData = {