Skip to content
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

pyln: Plugin methods and hooks refuse to set results twice #4094

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions contrib/pyln-client/pyln/client/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def __init__(self, plugin: 'Plugin', req_id: Optional[int], method: str,
self.plugin = plugin
self.state = RequestState.PENDING
self.id = req_id
self.termination_tb: Optional[str] = None

def getattr(self, key: str) -> Union[Method, Any, int]:
if key == "params":
Expand All @@ -84,21 +85,27 @@ def getattr(self, key: str) -> Union[Method, Any, int]:

def set_result(self, result: Any) -> None:
if self.state != RequestState.PENDING:
assert(self.termination_tb is not None)
raise ValueError(
"Cannot set the result of a request that is not pending, "
"current state is {state}".format(state=self.state))
"current state is {state}. Request previously terminated at\n"
"{tb}".format(state=self.state, tb=self.termination_tb))
self.result = result
self._write_result({
'jsonrpc': '2.0',
'id': self.id,
'result': self.result
})
self.state = RequestState.FINISHED
self.termination_tb = "".join(traceback.extract_stack().format()[:-1])

def set_exception(self, exc: Exception) -> None:
if self.state != RequestState.PENDING:
assert(self.termination_tb is not None)
raise ValueError(
"Cannot set the exception of a request that is not pending, "
"current state is {state}".format(state=self.state))
"current state is {state}. Request previously terminated at\n"
"{tb}".format(state=self.state, tb=self.termination_tb))
self.exc = exc
self._write_result({
'jsonrpc': '2.0',
Expand All @@ -111,6 +118,8 @@ def set_exception(self, exc: Exception) -> None:
"traceback": traceback.format_exc(),
},
})
self.state = RequestState.FAILED
self.termination_tb = "".join(traceback.extract_stack().format()[:-1])

def _write_result(self, result: dict) -> None:
self.plugin._write_locked(result)
Expand Down
40 changes: 40 additions & 0 deletions contrib/pyln-client/tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,3 +362,43 @@ def test1(msat: Millisatoshi):

ba = p._bind_pos(test1, ["100msat"], None)
test1(*ba.args, **ba.kwargs)


def test_duplicate_result():
p = Plugin(autopatch=False)

def test1(request):
request.set_result(1) # MARKER1
request.set_result(1)

req = Request(p, req_id=1, method="test1", params=[])
ba = p._bind_kwargs(test1, {}, req)
with pytest.raises(ValueError, match=r'current state is RequestState\.FINISHED(.*\n.*)*MARKER1'):
test1(*ba.args)

def test2(request):
request.set_exception(1) # MARKER2
request.set_exception(1)

req = Request(p, req_id=2, method="test2", params=[])
ba = p._bind_kwargs(test2, {}, req)
with pytest.raises(ValueError, match=r'current state is RequestState\.FAILED(.*\n*.*)*MARKER2'):
test2(*ba.args)
cdecker marked this conversation as resolved.
Show resolved Hide resolved

def test3(request):
request.set_exception(1) # MARKER3
request.set_result(1)

req = Request(p, req_id=3, method="test3", params=[])
ba = p._bind_kwargs(test3, {}, req)
with pytest.raises(ValueError, match=r'current state is RequestState\.FAILED(.*\n*.*)*MARKER3'):
test3(*ba.args)

def test4(request):
request.set_result(1) # MARKER4
request.set_exception(1)

req = Request(p, req_id=4, method="test4", params=[])
ba = p._bind_kwargs(test4, {}, req)
with pytest.raises(ValueError, match=r'current state is RequestState\.FINISHED(.*\n*.*)*MARKER4'):
test4(*ba.args)