-
-
Notifications
You must be signed in to change notification settings - Fork 724
chore(oxlint/napi): add clearLoadedPlugin function with automatic cleanup in ExternalLinter
#16057
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
4e4136f
ca5a920
3df2272
b998634
bf3b963
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| import { lintFile } from "./lint.js"; | ||
| import { loadPlugin } from "./load.js"; | ||
| import { clearLoadedPlugin, loadPlugin } from "./load.js"; | ||
|
|
||
| export { lintFile, loadPlugin }; | ||
| export { clearLoadedPlugin, lintFile, loadPlugin }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import { describe, it, expect } from "vitest"; | ||
| import { clearLoadedPlugin } from "../dist/index.js"; | ||
|
|
||
| /** | ||
| * Test the clearLoadedPlugin function | ||
| */ | ||
| describe("clearLoadedPlugin", () => { | ||
| it("should be exported from the main module", () => { | ||
| expect(clearLoadedPlugin).toBeDefined(); | ||
| expect(typeof clearLoadedPlugin).toBe("function"); | ||
| }); | ||
|
|
||
| it("should execute without errors", async () => { | ||
| // Call the clear function - it should not throw | ||
| await expect(clearLoadedPlugin()).resolves.toBeUndefined(); | ||
| }); | ||
|
|
||
| it("should work when called multiple times", async () => { | ||
| // Call it multiple times to ensure it doesn't error | ||
| await expect(clearLoadedPlugin()).resolves.toBeUndefined(); | ||
| await expect(clearLoadedPlugin()).resolves.toBeUndefined(); | ||
| await expect(clearLoadedPlugin()).resolves.toBeUndefined(); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -16,6 +16,8 @@ pub type ExternalLinterLintFileCb = Box< | |||||||||||||||||||||||||||
| + Send, | ||||||||||||||||||||||||||||
| >; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| pub type ExternalLinterClearLoadedPluginCb = Box<dyn Fn() + Send + Sync>; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| #[derive(Clone, Debug, Deserialize)] | ||||||||||||||||||||||||||||
| pub enum PluginLoadResult { | ||||||||||||||||||||||||||||
| #[serde(rename_all = "camelCase")] | ||||||||||||||||||||||||||||
|
|
@@ -47,14 +49,23 @@ pub struct JsFix { | |||||||||||||||||||||||||||
| pub struct ExternalLinter { | ||||||||||||||||||||||||||||
| pub(crate) load_plugin: ExternalLinterLoadPluginCb, | ||||||||||||||||||||||||||||
| pub(crate) lint_file: ExternalLinterLintFileCb, | ||||||||||||||||||||||||||||
| clear_loaded_plugin: ExternalLinterClearLoadedPluginCb, | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| impl ExternalLinter { | ||||||||||||||||||||||||||||
| pub fn new( | ||||||||||||||||||||||||||||
| load_plugin: ExternalLinterLoadPluginCb, | ||||||||||||||||||||||||||||
| lint_file: ExternalLinterLintFileCb, | ||||||||||||||||||||||||||||
| clear_loaded_plugin: ExternalLinterClearLoadedPluginCb, | ||||||||||||||||||||||||||||
| ) -> Self { | ||||||||||||||||||||||||||||
| Self { load_plugin, lint_file } | ||||||||||||||||||||||||||||
| Self { load_plugin, lint_file, clear_loaded_plugin } | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| impl Drop for ExternalLinter { | ||||||||||||||||||||||||||||
| fn drop(&mut self) { | ||||||||||||||||||||||||||||
| // Clear loaded plugin state when the linter is destroyed | ||||||||||||||||||||||||||||
| (self.clear_loaded_plugin)(); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
Comment on lines
+65
to
69
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The // Instead, use non-blocking mode and ignore errors
impl Drop for ExternalLinter {
fn drop(&mut self) {
// Use NonBlocking mode to avoid deadlocks during shutdown
let _ = (self.clear_loaded_plugin)();
}
}Or remove the Drop implementation entirely and require explicit cleanup by the caller.
Suggested change
Spotted by Graphite Agent |
||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The result of
cb.call()is silently discarded. If the JavaScript callback fails or throws an error, it will be ignored. Consider logging the error or adding a comment explaining why errors can be safely ignored during cleanup. This is especially important since this is called from theDropimplementation where panicking would be problematic.