Repository navigation
feat: execute registered flash tasks (CON-1833) #4
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
Merged
KAJdev
merged 4 commits into
main
from
zeke/con-1833-secp1-high-developer-rw-api-key-injected-into-every-deployed
Oct 30, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| """registered task loading and json-only execution.""" | ||
|
|
||
| import asyncio | ||
| import importlib | ||
| import inspect | ||
| import json | ||
| import os | ||
| import sys | ||
| import threading | ||
|
|
||
| from runpod_sdk_runtime import bootstrap | ||
| from runpod_sdk_runtime.executor import _await, execute_request | ||
|
|
||
| _loop = None | ||
| _loop_thread = None | ||
| _loop_lock = threading.Lock() | ||
|
|
||
|
|
||
| def _serve_loop(loop): | ||
| asyncio.set_event_loop(loop) | ||
| try: | ||
| loop.run_forever() | ||
| finally: | ||
| pending = asyncio.all_tasks(loop) | ||
| for task in pending: | ||
| task.cancel() | ||
| if pending: | ||
| loop.run_until_complete(asyncio.gather(*pending, return_exceptions=True)) | ||
| loop.run_until_complete(loop.shutdown_asyncgens()) | ||
| loop.run_until_complete(loop.shutdown_default_executor()) | ||
| loop.close() | ||
|
|
||
|
|
||
| def _run_awaitable(awaitable): | ||
| global _loop, _loop_thread | ||
| with _loop_lock: | ||
| if _loop is None: | ||
| _loop = asyncio.new_event_loop() | ||
| _loop_thread = threading.Thread( | ||
| target=_serve_loop, args=(_loop,), daemon=True | ||
| ) | ||
| _loop_thread.start() | ||
| future = asyncio.run_coroutine_threadsafe(_await(awaitable), _loop) | ||
| return future.result() | ||
|
|
||
|
|
||
| def close(): | ||
| global _loop, _loop_thread | ||
| with _loop_lock: | ||
| if _loop is None: | ||
| return | ||
| _loop.call_soon_threadsafe(_loop.stop) | ||
| _loop_thread.join() | ||
| _loop = None | ||
| _loop_thread = None | ||
|
|
||
|
|
||
| async def _initialize(handle): | ||
| result = handle._init_fn() | ||
| if inspect.isawaitable(result): | ||
| await result | ||
|
|
||
|
|
||
| def load_handle(): | ||
| bootstrap._validate_task_environment() | ||
| app_dir = os.environ.get("RUNPOD_APP_DIR", "/app") | ||
| entry = bootstrap._task_resource(bootstrap._manifest(app_dir)) | ||
| if app_dir not in sys.path: | ||
| sys.path.insert(0, app_dir) | ||
| module = importlib.import_module(entry["module"]) | ||
|
|
||
| from runpod.apps.handles import FunctionHandle | ||
| from runpod.apps.spec import ResourceKind | ||
|
|
||
| target = module | ||
| for name in entry["qualname"].split("."): | ||
| target = getattr(target, name, None) | ||
| handles = { | ||
| value | ||
| for value in [target, *vars(module).values()] | ||
| if isinstance(value, FunctionHandle) | ||
| and value.spec.name == entry["name"] | ||
| and value.spec.kind == ResourceKind.TASK | ||
| and value._app.name == os.environ["FLASH_APP_NAME"] | ||
| and value._fn.__qualname__ == entry["qualname"] | ||
| } | ||
| if len(handles) != 1: | ||
| raise RuntimeError("module must expose exactly one matching @app.task handle") | ||
| handle = handles.pop() | ||
| if handle._init_fn is not None: | ||
| _run_awaitable(_initialize(handle)) | ||
| return handle | ||
|
|
||
|
|
||
| def validate_request(request, *, timeout_only=False): | ||
| allowed = ( | ||
| {"timeout"} | ||
| if timeout_only | ||
| else {"args", "kwargs", "serialization_format", "timeout"} | ||
| ) | ||
| if request.keys() - allowed: | ||
| raise ValueError("deployed tasks accept only json args, kwargs and timeout") | ||
| if request.get("serialization_format", "json") != "json": | ||
| raise ValueError("deployed tasks require json serialization") | ||
| if not isinstance(request.get("args", []), list): | ||
| raise ValueError("args must be a json array") | ||
| if not isinstance(request.get("kwargs", {}), dict): | ||
| raise ValueError("kwargs must be a json object") | ||
| json.dumps(request, allow_nan=False) | ||
|
|
||
|
|
||
| def execute(handle, request): | ||
| return execute_request( | ||
| {"serialization_format": "json"}, | ||
| prepared=(handle._fn, request.get("args", []), request.get("kwargs", {})), | ||
| run_awaitable=_run_awaitable, | ||
| ) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.