Skip to content
This repository was archived by the owner on Sep 8, 2025. It is now read-only.

Commit cfb06e9

Browse files
committed
Fix side effect of plugin discovery
Previously, when the main.py file is loaded it implicitly caused a plugin discovery as a side effect. This becomes a problem when external plugins derive from `BaseIsolatedPlugin` which causes the plugin to spawn a new process. That is, because the new process implicitly loads main.py which then causes another plugin discovery to run in the plugin process. Trivia: This does not happen when the process launching strategy is `fork` because then, the forked process does already come loaded with the memory of the parent process and therefore does not load main.py again.
1 parent 7b37d91 commit cfb06e9

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

trinity/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
SharedProcessScope,
5252
)
5353
from trinity.plugins.registry import (
54-
ALL_PLUGINS,
54+
get_all_plugins,
5555
)
5656
from trinity.utils.ipc import (
5757
wait_for_ipc,
@@ -377,6 +377,6 @@ async def handle_networking_exit(service: BaseService,
377377
def setup_plugins(scope: BaseManagerProcessScope) -> PluginManager:
378378
plugin_manager = PluginManager(scope)
379379
# TODO: Implement auto-discovery of plugins based on some convention/configuration scheme
380-
plugin_manager.register(ALL_PLUGINS)
380+
plugin_manager.register(get_all_plugins())
381381

382382
return plugin_manager

trinity/plugins/registry.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import pkg_resources
2+
from typing import (
3+
Tuple,
4+
)
25

6+
from trinity.extensibility import (
7+
BasePlugin,
8+
)
39
from trinity.plugins.builtin.attach.plugin import (
410
AttachPlugin
511
)
@@ -19,10 +25,6 @@
1925
LightPeerChainBridgePlugin
2026
)
2127

22-
from trinity.plugins.examples.peer_count_reporter.plugin import (
23-
PeerCountReporterPlugin
24-
)
25-
2628

2729
def is_ipython_available() -> bool:
2830
try:
@@ -33,24 +35,21 @@ def is_ipython_available() -> bool:
3335
return True
3436

3537

36-
# This is our poor mans central plugin registry for now. In the future,
37-
# we'll be able to load plugins from some path and control via Trinity
38-
# config file which plugin is enabled or not
39-
40-
BUILTIN_PLUGINS = [
38+
BUILTIN_PLUGINS = (
4139
AttachPlugin() if is_ipython_available() else AttachPlugin(use_ipython=False),
4240
EthstatsPlugin(),
4341
FixUncleanShutdownPlugin(),
4442
JsonRpcServerPlugin(),
4543
LightPeerChainBridgePlugin(),
4644
TxPlugin(),
47-
PeerCountReporterPlugin(),
48-
]
45+
)
46+
47+
48+
def discover_plugins() -> Tuple[BasePlugin, ...]:
49+
# Plugins need to define entrypoints at 'trinity.plugins' to automatically get loaded
50+
# https://packaging.python.org/guides/creating-and-discovering-plugins/#using-package-metadata
4951

50-
# Plugins need to define entrypoints at 'trinity.plugins' to automatically get loaded
51-
# https://packaging.python.org/guides/creating-and-discovering-plugins/#using-package-metadata
52-
DISCOVERED_PLUGINS = [
53-
entry_point.load()() for entry_point in pkg_resources.iter_entry_points('trinity.plugins')
54-
]
52+
return tuple(entry_point.load()() for entry_point in pkg_resources.iter_entry_points('trinity.plugins'))
5553

56-
ALL_PLUGINS = BUILTIN_PLUGINS + DISCOVERED_PLUGINS
54+
def get_all_plugins() -> Tuple[BasePlugin, ...]:
55+
return BUILTIN_PLUGINS + discover_plugins()

0 commit comments

Comments
 (0)