-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix issues with cyclic references and plugin instances not being GC'ed (
#56) ApiWrapper receives a weak reference to the plugin instance to break the cyclic dependency. The handler passed to ApiWrapper methods that create API handlers is only referenced weakly.
- Loading branch information
Showing
4 changed files
with
97 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from .weak_method import weak_method | ||
|
||
__all__ = [ | ||
'weak_method', | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from LSP.plugin.core.typing import Any, Callable | ||
from types import MethodType | ||
import weakref | ||
|
||
|
||
__all__ = ['weak_method'] | ||
|
||
|
||
# An implementation of weak method borrowed from sublime_lib [1] | ||
# | ||
# We need it to be able to weak reference bound methods as `weakref.WeakMethod` is not available in | ||
# 3.3 runtime. | ||
# | ||
# The reason this is necessary is explained in the documentation of `weakref.WeakMethod`: | ||
# > A custom ref subclass which simulates a weak reference to a bound method (i.e., a method defined | ||
# > on a class and looked up on an instance). Since a bound method is ephemeral, a standard weak | ||
# > reference cannot keep hold of it. | ||
# | ||
# [1] https://github.com/SublimeText/sublime_lib/blob/master/st3/sublime_lib/_util/weak_method.py | ||
|
||
def weak_method(method: Callable) -> Callable: | ||
assert isinstance(method, MethodType) | ||
self_ref = weakref.ref(method.__self__) | ||
function_ref = weakref.ref(method.__func__) | ||
|
||
def wrapped(*args: Any, **kwargs: Any) -> Any: | ||
self = self_ref() | ||
function = function_ref() | ||
if self is None or function is None: | ||
print('[lsp_utils] Error: weak_method not called due to a deleted reference', [self, function]) | ||
return | ||
return function(self, *args, **kwargs) | ||
|
||
return wrapped |