{"record":{"id":"5bf979dc55d68da5","repo":"siyuan-note/siyuan","slug":"plugin-stopped-w","errorCode":null,"errorMessage":"plugin stopped: %w","messagePattern":"plugin stopped: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"kernel/plugin/plugin.go","lineNumber":776,"sourceCode":"\t\t\tif !ok {\n\t\t\t\treturn\n\t\t\t}\n\t\t\tlogging.LogErrorf(\"[plugin:%s] storage watcher error: %s\", p.Name, err)\n\t\t}\n\t}\n}\n\n// addStorageWatch adds a path to the fsnotify watcher to watch for storage file/directory changes.\nfunc (p *KernelPlugin) addStorageWatch(path string) (err error) {\n\tif !isPluginFileWatchSupported() {\n\t\treturn errPluginFileWatchUnsupported\n\t}\n\n\tp.watcherMu.Lock()\n\tdefer p.watcherMu.Unlock()\n\n\tif contextErr := p.context.Err(); contextErr != nil {\n\t\treturn fmt.Errorf(\"plugin stopped: %w\", contextErr)\n\t}\n\tif p.watcher == nil {\n\t\tp.watcher, err = fsnotify.NewWatcher()\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"initialize fsnotify watcher: %w\", err)\n\t\t}\n\t\tp.watcherDone = make(chan struct{})\n\t\tgo p.startStorageWatch(p.watcher, p.watcherDone)\n\t}\n\n\terr = p.watcher.Add(path)\n\treturn\n}\n\n// removeStorageWatch removes a path from the fsnotify watcher to stop watching for storage file/directory changes.\nfunc (p *KernelPlugin) removeStorageWatch(path string) (err error) {\n\tif !isPluginFileWatchSupported() {\n\t\treturn errPluginFileWatchUnsupported","sourceCodeStart":758,"sourceCodeEnd":794,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/plugin/plugin.go#L758-L794","documentation":"addStorageWatch registers a path on the plugin's fsnotify watcher. Before doing so it checks p.context.Err(); if the plugin's context is already cancelled (plugin stopped/unloaded), it refuses with 'plugin stopped: <context error>'. This prevents watching files for a plugin that is no longer running.","triggerScenarios":"Calling addStorageWatch (indirectly via storage watch APIs) after the plugin has been stopped or unloaded, or racing a stop with a watch registration. Also surfaced in tests that exercise lazy watcher creation after context cancellation.","commonSituations":"Async callbacks firing after plugin unload; storage watch requests queued while the plugin is being disabled; race between plugin shutdown and lazy watcher initialization.","solutions":["Do not register storage watches after plugin unload; cancel pending async work on unload","Check plugin lifecycle state before calling watch APIs","Serialize shutdown so watches are removed before the context is cancelled","In tests, create the watcher before cancelling the plugin context"],"exampleFix":"// before\nplugin.addStorageWatch(path); // may run after unload\n\n// after\nif (!plugin.stopped) {\n  plugin.addStorageWatch(path);\n}","handlingStrategy":"try-catch","validationCode":"if (plugin.context?.signal?.aborted) {\n  throw new Error(\"plugin stopped; cannot add storage watch\");\n}","typeGuard":null,"tryCatchPattern":"try {\n  plugin.addStorageWatch(path);\n} catch (e) {\n  if (String(e).startsWith(\"plugin stopped\")) {\n    return; // plugin already unloaded; skip registration\n  }\n  throw e;\n}","preventionTips":["Cancel pending async work when the plugin unloads","Register watches only during active plugin lifetime","Serialize shutdown so watches are cleaned before context cancellation"],"tags":["plugin","lifecycle","fsnotify","watcher"],"backgroundTag":"invalid-state-transition","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}