ElementsProject / lightning

Core Lightning — Lightning Network implementation focusing on spec compliance and performance
Other
2.83k stars 899 forks source link

If plugins use std.out without proper json nothing is displayed #2146

Closed renepickhardt closed 5 years ago

renepickhardt commented 5 years ago

Issue and Steps to Reproduce

when I write a plugin in python and I use print("test") in the best case the plugin does not start and give me an error because of: https://github.com/ElementsProject/lightning/blob/b7da41e674c06c4101b389842a14f695cb29520e/lightningd/plugin.c#L294

In the worst case this happens somewhere during the plugin and I will see nothing. Not on the shell and not in the logfiles.

I tried to provide a patch to plugin.c but it seems like I did not find the correct place. It seems like plugin->buffer is not emptied but when adding a log_debug(plugin->log, "received message: %s", plugin->buffer); it did not get displayed. Though it worked when adding it to other places within plugin.c.

This is particularly annoying since I am currently developing a small plugin that starts a flask server (not sure if plugins like this are actually a good idea at all) and developing becomes really cumbersome because every time flask crashes with internal server error or something similar I don't see any output.

Also when having syntax errors in python or exceptions I can't see any output. This is really really annoying.

My current workflow is that while developing the plugin I am mocking my c-lightning node and just run the plugin from the command line. But I guess that this should only be a temporary solution.

cdecker commented 5 years ago

I'm working on an extension of the pylightning library that provides some common infrastructure for plugins. Part of that is monkey-patching sys.stdout to send correct notifications that are then added to the logs of lightningd. So with that you'll be able to use print and exception backtraces from within the plugin like everywhere else. Just give me a couple of days to write and test it :-)

renepickhardt commented 5 years ago

I am surprised you do this on the python side. Also I never heard the word monkey patching. If you give me a high level idea how you would do that I could probably help out so that you can focus more on stuff happening on the c-side (:

ZmnSCPxj commented 5 years ago

Monkey patching, is modifying a field of the object representing a class, or other modification of "system" details. It is possible in some languages with excessive dynamism to just modify "system" objects, or to be able to access the interpreter details regarding classes or types.

This is generally a bad idea except when it is a good idea.

The term I believe is common in JavaScript, where objects have a prototype chain, and modifying an object along the prototype chain implicitly "modifies" every object dependent on (derived from or instantiating from) it.

renepickhardt commented 5 years ago

This is generally a bad idea except when it is a good idea.

( :

rustyrussell commented 5 years ago

We now log when we get bad JSON from a plugin. Does that resolve this issue?

renepickhardt commented 5 years ago

as far as I experienced it I didn't have trouble anymore (at least with the python client api)

cdecker commented 5 years ago

as far as I experienced it I didn't have trouble anymore (at least with the python client api)

Done, push to prod! :wink: