Skip to content

Commit 8841a69

Browse files
committed
Ensure isolated plugins create pidfiles
Without this, when an isolated plugin creates a process, we do not track it with a pidfile. That in turn can lead to dangling processes which do not get cleaned up with the fix-unclean-shutdown command.
1 parent 71b595c commit 8841a69

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

trinity/_utils/os.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import re
12
import resource
3+
import unicodedata
24

35

46
def get_open_fd_limit() -> int:
@@ -7,3 +9,17 @@ def get_open_fd_limit() -> int:
79
"""
810
soft_limit, hard_limit = resource.getrlimit(resource.RLIMIT_NOFILE)
911
return soft_limit
12+
13+
14+
def friendly_filename_or_url(value: str) -> str:
15+
"""
16+
Normalize any string to be file name and URL friendly.
17+
Convert to lowercase, remove non-alpha characters,
18+
and convert spaces to hyphens.
19+
"""
20+
# Taken from:
21+
# https://stackoverflow.com/questions/295135/turn-a-string-into-a-valid-filename/295466#295466
22+
value = str(unicodedata.normalize('NFKD', value).encode('ascii', 'ignore'))
23+
value = str(re.sub('[^\w\s-]', '', value).strip().lower())
24+
value = str(re.sub('[-\s]+', '-', value))
25+
return value

trinity/extensibility/plugin.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@
5050
from trinity._utils.logging import (
5151
setup_queue_logging,
5252
)
53+
from trinity._utils.os import (
54+
friendly_filename_or_url,
55+
)
5356

5457

5558
class PluginStatus(Enum):
@@ -135,6 +138,13 @@ def name(self) -> str:
135138
"""
136139
pass
137140

141+
@property
142+
def normalized_name(self) -> str:
143+
"""
144+
The normalized (computer readable) name of the plugin
145+
"""
146+
return friendly_filename_or_url(self.name)
147+
138148
@property
139149
def logger(self) -> logging.Logger:
140150
"""
@@ -296,7 +306,8 @@ def _prepare_start(self) -> None:
296306
self.event_bus.broadcast(
297307
PluginStartedEvent(type(self))
298308
)
299-
self.do_start()
309+
with self.context.trinity_config.process_id_file(self.normalized_name):
310+
self.do_start()
300311

301312
def stop(self) -> None:
302313
"""

0 commit comments

Comments
 (0)