Startup Flow and Manual Control
Tip
When to read this: When you need to manually drive the startup chain, customize the startup order, or troubleshoot startup failures. Skip if not needed; it does not affect the use of other features.
ErisPulse's await sdk.run() / await sdk.init() encapsulates the entire startup chain into a "one-liner." However, when you need to fully customize the startup process (e.g., partial loading, dynamic registration, hot-swapping, injecting custom loading strategies), you need to understand what happens inside this chain and how to manually drive each step.
This article breaks down the startup chain into independent components, explains their respective responsibilities, invocation order, and provides examples for manually initiating the entire process.
This article assumes you have run through the first bot and understand the two
keep_runningmodes ofsdk.run(). This article focuses on the internal breakdown of theinit()chain, as well as lower-level entry points such asinit()/init_task()/init_sync().
Overview of SDK Top-Level Entry Points
In addition to the two keep_running modes of run(), the SDK provides several lower-level initialization entry points, differing in asynchronicity, return values, and whether exceptions are wrapped:
| Entry Point | Asynchronicity | Return Value | Exception Handling | Use Case |
|---|---|---|---|---|
await sdk.run(True) |
async, blocks to maintain | None (automatically uninit on shutdown) |
Module/adapter errors are intercepted, not crashing the process | Pure bot application |
await sdk.run(False) |
async, non-blocking | None (no automatic uninstall) |
Same as above | Initialize and then execute custom logic |
await sdk.init() |
async, requires await | bool |
Internal catch of component exceptions, returns False on failure |
Manual lifecycle control (paired with uninit()) |
sdk.init_task() |
async, returns Task without blocking | asyncio.Task |
Same as init() |
Concurrent initialization or when event loop is not running |
sdk.init_sync() |
Synchronous, blocks current thread | bool |
Same as init() |
Command-line scripts, synchronous entry without event loop |
Common misconception:
await sdk.init()is not equivalent toawait sdk.run(keep_running=False). Two differences: ①init()returnsbool(returnsFalseon failure),run()returnsNone; ②init()only performs initialization, does not automatically uninstall, whilerun()automaticallyuninit()when the event loop ends. Therefore, when you need to manually pair uninstallation or customize the lifecycle, useinit()+uninit().
Overview of the Startup Chain
sdk.init() (specifically its internal Initializer.init()) launches the entire framework in the following order:
flowchart TD
A[0. Prepare environment<br/>Configuration loading / Exception handling] --> B
B[1. Parallel discovery and loading<br/>AdapterLoader.load / ModuleLoader.load<br/>Internal call to Finder.find_all] --> C
C[2. Register adapters<br/>AdapterLoader.register_to_manager] --> D
D[3. Start adapters<br/>adapter.startup] --> E
E[4. Register modules<br/>ModuleLoader.register_to_manager] --> F
F[5. Initialize modules<br/>ModuleLoader.initialize_modules<br/>Instantiation and mounting to sdk] --> G
G[6. Start routing server<br/>router.start]
Corresponding core components:
| Layer | Component | Responsibility |
|---|---|---|
| Discovery | AdapterFinder / ModuleFinder |
Discover adapters/modules from entry-points of installed packages |
| Loading | AdapterLoader / ModuleLoader |
Discovery + import + metadata reading + enable/disable judgment, returns object list |
| Registration | *Loader.register_to_manager |
Register objects to corresponding managers |
| Management | sdk.adapter / sdk.module |
Maintain adapter/module instances, provide start/stop interfaces |
| Initialization | ModuleLoader.initialize_modules |
Create module instances and mount to sdk (handle dependency topological sorting) |
| Routing | sdk.router |
HTTP / WebSocket server |
Important:
FinderandLoaderare two layers.Loaderinternally already holds aFinder(e.g.,AdapterLoaderhas aAdapterFinder,ModuleLoaderhas aModuleFinder). In most scenarios, you only need to useLoader; you only useFinderalone when you need to "list without importing."
Detailed Component Explanations
1. Discovery Layer: Finder
Finder only finds which packages provide adapters/modules, without importing or instantiating.
from ErisPulse.finders import AdapterFinder, ModuleFinder
adapter_finder = AdapterFinder()
module_finder = ModuleFinder()
# Find all installed adapters/modules entry-points
adapter_entries = adapter_finder.find_all() # list[EntryPoint]
module_entries = module_finder.find_all() # list[EntryPoint]
# Find a single one by name
entry = module_finder.find_by_name("MyModule") # EntryPoint | None
Each EntryPoint can be .load() to get the corresponding class, but usually you don't need to call it manually—Loader will do it.
2. Loading Layer: Loader
Loader does "import + metadata reading + enable/disable judgment" on top of Finder.
from ErisPulse.loaders import AdapterLoader, ModuleLoader
from ErisPulse import sdk
adapter_loader = AdapterLoader()
module_loader = ModuleLoader()
# load() internally: calls finder.find_all() → processes each entry-point → returns a triple
adapter_objs, enabled_adapters, disabled_adapters = await adapter_loader.load(sdk.adapter)
module_objs, enabled_modules, disabled_modules = await module_loader.load(sdk.module)
The three-tuple returned by load():
| Return Value | Meaning |
|---|---|
objs (dict) |
Name → object (adapter class / module wrapper object) |
enabled (list[str]) |
Names that are enabled (not disabled in configuration) |
disabled (list[str]) |
Names that are disabled |
Diagnostic Information on Loading Failures
When a module/adapter throws an exception during loading or initialization, the framework skips that component and continues loading other components, while outputting a summary of user code frames, allowing you to locate the error position at the default INFO level without manually re-enabling DEBUG:
[ERROR] [ModuleLoader] Failed to load module MyModule from entry-point, skipped: 'NoneType' object has no attribute 'platform'
→ MyModule/Core.py:42 in on_load
adapter = sdk.platform
→ AttributeError: 'NoneType' object has no attribute 'platform'
→ Note: Increase log level to DEBUG to view full stack trace; check the implementation code of module MyModule
The diagnostic information is generated by the ErisPulse.runtime.diagnostics module, automatically filtering out internal framework frames and retaining only your code frames. If you need to reuse it in custom loading logic:
from ErisPulse.runtime import log_diagnostic
try:
risky_init()
except Exception as e:
log_diagnostic(e) # Automatically extract user code frames and write to ERROR log
This module also provides two low-level functions: extract_user_frame() (returns structured frame information) and format_diagnostic_block() (returns multi-line text).
3. Registration Layer: register_to_manager
Registers the objects produced by Loader to the manager so that sdk.adapter / sdk.module can recognize them.
# Register adapters (returns bool, indicating whether all succeeded)
await adapter_loader.register_to_manager(enabled_adapters, adapter_objs, sdk.adapter)
# Register modules
await module_loader.register_to_manager(enabled_modules, module_objs, sdk.module)
After registration, adapters are registered to the adapter manager, and modules are registered to the module manager, but they are not started/initialized yet.
4. Start Adapters
# Start all registered adapters
await sdk.adapter.startup()
# Or specify a platform
await sdk.adapter.startup("yunhu")
await sdk.adapter.startup(["yunhu", "telegram"])
Registration ≠ Startup.
register_to_manageronly registers;startupcalls the adapter'sstart(), establishing a connection to the platform.
5. Initialize Modules
Modules have an extra step—instantiation and mounting to sdk (so you can call sdk.MyModule.xxx). This step also handles module dependencies and topological sorting.
success = await module_loader.initialize_modules(
enabled_modules, module_objs, sdk.module, sdk
)
After successful instantiation, modules appear on sdk.<ModuleName>.
6. Start Routing Server
await sdk.router.start(
host="0.0.0.0",
port=8000,
ssl_certfile=None,
ssl_keyfile=None,
)
The routing server is responsible for receiving webhook/WebSocket callbacks from adapters. Without starting it, server-mode adapters cannot receive messages.
Complete Manual Startup Example
The following code is equivalent to the core process of await sdk.init(), but each step is exposed to you, allowing you to insert custom logic at any step:
import asyncio
from ErisPulse import sdk
from ErisPulse.loaders import AdapterLoader, ModuleLoader
async def manual_startup():
# 0. Prepare environment (load configuration, register global exception handler)
# _prepare_environment is a pre-step inside init(); in manual flow, it must be called first,
# otherwise Loader won't read the configuration and will misjudge all adapters/modules as disabled.
if not await sdk._prepare_environment():
print("Environment preparation failed")
return False
# 1. Create loaders (each internally holds a Finder)
adapter_loader = AdapterLoader()
module_loader = ModuleLoader()
# 2. Parallel discovery and loading (consistent with internal use in init())
(adapter_objs, enabled_adapters, disabled_adapters), \
(module_objs, enabled_modules, disabled_modules) = await asyncio.gather(
adapter_loader.load(sdk.adapter),
module_loader.load(sdk.module),
)
# 3. Register adapters
await adapter_loader.register_to_manager(
enabled_adapters, adapter_objs, sdk.adapter
)
# 4. Start adapters
if enabled_adapters:
await sdk.adapter.startup()
# 5. Register modules
await module_loader.register_to_manager(
enabled_modules, module_objs, sdk.module
)
# 6. Initialize modules (instantiation + mounting to sdk)
if enabled_modules:
await module_loader.initialize_modules(
enabled_modules, module_objs, sdk.module, sdk
)
# 7. Start routing server
await sdk.router.start(host="0.0.0.0", port=8000)
print("Manual startup completed")
return True
async def main():
ok = await manual_startup()
if ok:
# Block to maintain running (manual flow does not automatically block)
await asyncio.Event().wait()
if __name__ == "__main__":
asyncio.run(main())
When to Use Manual Startup?
In most cases, manual startup is not required; await sdk.run() already handles all the above. Manual startup is only valuable in these scenarios:
- Partial loading: Load only specified adapters/modules, skipping others
- Dynamic registration: Register new adapters/modules at runtime based on conditions
- Custom order: Need to disrupt the default loading order (e.g., start a module before an adapter)
- Inject strategies: Inject custom strict mode managers, loading strategies, etc. into Loader
- Debugging/diagnosis: Manually drive at a certain step when a failure occurs to locate the issue
Runtime Fine-Grained Control
Even after using sdk.run() to complete the startup, you can still individually control subsystems at runtime without restarting the entire SDK:
Hot Restart of Adapters
# Hot restart a specific adapter (fix connection, does not affect other platforms)
await sdk.adapter.shutdown("yunhu")
await sdk.adapter.startup("yunhu")
# Dynamically start a new platform
await sdk.adapter.startup("telegram")
# Temporarily take a platform offline
await sdk.adapter.shutdown("telegram")
adapter.startup()requires the adapter to be registered to the manager. Registration occurs duringinit()/run(), so this is fine-grained control after startup.
Routing Server
# Temporarily take the webhook server offline
await sdk.router.stop()
# Restart (e.g., change port)
await sdk.router.start(host="0.0.0.0", port=9000)
Modules on Demand
# Manually load a module (possibly lazy-loaded)
await sdk.load_module("MyModule")
Graceful Shutdown
Since version 2.7.0, sdk.shutdown() provides programmatic graceful shutdown: set a shutdown event to let the main loop, which is hanging due to await sdk.run(keep_running=True), return, thus triggering uninit() to complete resource cleanup.
# Call from any coroutine to trigger graceful exit (run() hangs and returns, then automatically uninit)
sdk.shutdown()
Typical use cases:
async def shutdown_after_idle():
await asyncio.sleep(3600)
sdk.shutdown() # Graceful exit after 1 hour of idle
Signal Handling: run() internally registers SIGTERM / SIGHUP handlers, converting system signals into graceful shutdown—when containers (Docker docker stop) or systemd stop services, the process completes uninit() cleanup instead of being forcibly killed.
- Windows does not support
loop.add_signal_handler, so the signal handler is automatically skipped (still usesdk.shutdown()or Ctrl+C to trigger shutdown) - Repeatedly calling
sdk.shutdown()is safe (no operation if the event is already set)
Unload Process
The reverse operation of startup is await sdk.uninit(), which cleans up in the reverse order:
- Close all adapters (
adapter.shutdown()) - Unload all modules
- Clean up all event handlers
- Clean up managers and module attributes on SDK
In manual startup scenarios, remember to call uninit() before exiting to ensure graceful shutdown:
try:
await asyncio.Event().wait() # Maintain running
finally:
await sdk.uninit()
Restart
The SDK provides two restart methods, both not requiring you to manually unload first—the framework handles it itself:
| Method | Call | Behavior | Use Case |
|---|---|---|---|
| Hot Restart | await sdk.restart() |
Code-level hot reload at the component layer within the same process: all modules go through a unified reload pipeline (global syntax pre-check / snapshot rollback / identity verification), adapters first re-discover entry-points (newly installed/uninstalled adapters during runtime take effect immediately), then reload each component code-level | Pip upgrades or installing components to make code effective, hot reload of modules |
| Hard Restart | await sdk.hard_restart() |
uninit() followed by exiting the process with exit code 42, restarted by an external supervisor |
Framework self-upgrade (new framework code only takes effect in new process), C extension side effects, suspected resource leaks |
# Hot Restart: Component-level code hot reload (most commonly used; newly installed adapters during runtime are also pulled in)
await sdk.restart()
# Hard Restart: Exit the process, handed over to an external supervisor (see "Supervisor Guide" below)
await sdk.hard_restart("sdk self-update") # Optional reason for supervisor display
Two points to note:
- These methods execute the restart in a background task, immediately returning
Trueto indicate "restart task scheduled", not "restart completed." The actual restart runs in the background, avoiding interruption of the current event chain.- The principle of
hard_restart()is: after unloading and flushing configuration, exit the process with exit code 42 (HARD_RESTART_EXIT_CODE)—it does not start a new process itself. It must be restarted by an external supervisor detecting exit code 42. If you directly runpython main.pywithout any supervisor, the process exits with code 42 and does not automatically restart (the framework will warn).
Mutual Exclusion and Merging of Hot Restarts (since 2.11.1): All hot reload operations (single module reload / full reload / adapter reload / sleep / new plugin hot load) are mutually exclusive throughout the process—consecutive triggers during save storms do not concurrently cross, and during an ongoing restart, new requests are automatically merged (requests falling within the ordinary reload lock period are also merged and queued), and after completion, at most one additional round is run. When files with syntax errors are saved, the entire reload will be rejected by a pre-check before unloading (listing all error files and line numbers at once), adapter layer skips the current reload,
restartfails, and all old instances continue serving; after fixing,restartagain.
When to Use Hard Restart?
Hot restart covers most scenarios (pip upgrades/installing components, hot reloading module code, including newly installed adapters during runtime). The following scenarios require hard restart:
- Framework self-upgrade: After upgrading ErisPulse itself, the new framework code only takes effect in a new process (Python does not re-import already loaded framework code).
- Binary library (C extension) side effects: Hot restart occurs within the same process and cannot release C extensions, open file descriptors, threads, and other process-level resources; hard restart switches to a new process, completely clearing these side effects.
- Resource leak diagnosis: When suspected memory or handle leaks exist, hard restart provides a clean environment.
The "Framework Restart" function in the Dashboard management panel internally calls
hard_restart().
Exit Code 42 Contract
Hard restart is cross-process collaboration: SDK is responsible for exiting (code 42), supervisor is responsible for restarting.
| Role | Behavior |
|---|---|
| SDK (when hard restarted) | uninit() → flush configuration → write hard restart marker (reason / PID, for supervisor display) → os._exit(42) |
| Supervisor | Detects child process exit code 42 → displays hard restart reason → restarts the same command |
sdk.is_supervised()can query whether the current process was started by a supervisor (detects environment variableERISPULSE_SUPERVISED). CLIruncommand starts a subprocess and automatically injects this marker; external supervisors like systemd / Docker do not inject it, sois_supervised()returnsFalse, and the framework will warn "no supervisor detected" after hard restart.
Supervisor Guide
Choose a supervisor suitable for you to make hard restart truly effective:
1. CLI run command (development/simple deployment, recommended)
epsdk run main.py has a built-in supervision loop: detects the child process exit code, restarts immediately if code 42 (and displays the hard restart reason); other abnormal exit codes retry automatically with exponential backoff; Ctrl+C first gracefully terminates the child process (code 0 is considered normal exit, no restart).
epsdk run main.py
epsdk run --reload (development hot-reload mode, since 2.11.1) also uses the "supervisor process + reload worker subprocess" structure: worker saves .py files to trigger component-level hot reload (not full framework restart); hard restart is handled by the supervisor process restarting a new worker, making new code effective after upgrading the framework.
2. systemd (Linux server)
RestartForceExitStatus=42 makes exit code 42 trigger a restart (default on-failure only works for non-zero codes):
[Service]
ExecStart=/usr/bin/python3 /opt/mybot/main.py
Restart=on-failure
RestartForceExitStatus=42
RestartSec=2
User=mybot
3. Docker / docker-compose
The PID 1 inside the container is the application process, and after exit code 42, the container exits—use restart policy to automatically restart it:
services:
bot:
build: .
restart: unless-stopped # Restart for any exit (including 42)
4. PM2 (Node ecosystem operations)
pm2 start main.py --name mybot --interpreter python3
# 42 is treated as an exit code, PM2 defaults to restart; set restart_delay to debounce
pm2 set mybot.restart_delay 2000
5. supervisord
[program:mybot]
command=python3 /opt/mybot/main.py
autorestart=true
exitcodes=0,2,42 # 42 is also considered "normal exit requiring restart"
6. Pure Python Custom Supervisor
import subprocess, sys, time
while True:
p = subprocess.Popen([sys.executable, "main.py"])
code = p.wait()
if code == 42: # Hard restart request
time.sleep(0.5)
continue
if code == 0: # Normal exit
break
time.sleep(3) # Abnormal exit, retry with backoff
Behavior without a supervisor: Running directly with
python main.py, callinghard_restart()exits the process with code 42 and does not restart. In this case, you should integrate any of the above supervisors.
Related Documentation
- Create Your First Bot - Introduction to the two basic
keep_runningmodes - Lifecycle Management - Listen to startup events such as
core.init.start/core.init.complete - Lazy Loading System - Module lazy loading mechanism and
load_module