vlambda 0.2.3__tar.gz → 0.2.4__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {vlambda-0.2.3 → vlambda-0.2.4}/PKG-INFO +1 -1
- {vlambda-0.2.3 → vlambda-0.2.4}/pyproject.toml +1 -1
- vlambda-0.2.4/python/vlambda/__init__.py +8 -0
- {vlambda-0.2.3 → vlambda-0.2.4}/python/vlambda/__main__.py +5 -3
- {vlambda-0.2.3 → vlambda-0.2.4}/python/vlambda/pool.py +26 -0
- {vlambda-0.2.3 → vlambda-0.2.4}/python/vlambda/request.py +16 -0
- {vlambda-0.2.3 → vlambda-0.2.4}/python/vlambda/runner.py +18 -3
- {vlambda-0.2.3 → vlambda-0.2.4}/python/vlambda/sandbox.py +2 -14
- {vlambda-0.2.3 → vlambda-0.2.4}/src/vlambda_native.v +9 -5
- {vlambda-0.2.3 → vlambda-0.2.4}/tests/test_vlambda.py +10 -0
- {vlambda-0.2.3 → vlambda-0.2.4}/vcraft.toml +1 -1
- vlambda-0.2.3/python/vlambda/__init__.py +0 -3
- {vlambda-0.2.3 → vlambda-0.2.4}/LICENSE +0 -0
- {vlambda-0.2.3 → vlambda-0.2.4}/README.md +0 -0
- {vlambda-0.2.3 → vlambda-0.2.4}/python/vlambda/assets.py +0 -0
- {vlambda-0.2.3 → vlambda-0.2.4}/python/vlambda/auth.py +0 -0
- {vlambda-0.2.3 → vlambda-0.2.4}/python/vlambda/cache.py +0 -0
- {vlambda-0.2.3 → vlambda-0.2.4}/python/vlambda/dbpool.py +0 -0
- {vlambda-0.2.3 → vlambda-0.2.4}/python/vlambda/metrics.py +0 -0
- {vlambda-0.2.3 → vlambda-0.2.4}/python/vlambda/packages.py +0 -0
- {vlambda-0.2.3 → vlambda-0.2.4}/python/vlambda/project.py +0 -0
- {vlambda-0.2.3 → vlambda-0.2.4}/python/vlambda/py.typed +0 -0
- {vlambda-0.2.3 → vlambda-0.2.4}/python/vlambda/reset.py +0 -0
- {vlambda-0.2.3 → vlambda-0.2.4}/python/vlambda/sandbox_os.py +0 -0
- {vlambda-0.2.3 → vlambda-0.2.4}/python/vlambda/shared_cache.py +0 -0
- {vlambda-0.2.3 → vlambda-0.2.4}/python/vlambda/tasks.py +0 -0
- {vlambda-0.2.3 → vlambda-0.2.4}/python/vlambda/transfer.py +0 -0
- {vlambda-0.2.3 → vlambda-0.2.4}/tests/conftest.py +0 -0
- {vlambda-0.2.3 → vlambda-0.2.4}/tests/test_unit.py +0 -0
- {vlambda-0.2.3 → vlambda-0.2.4}/v.mod +0 -0
- {vlambda-0.2.3 → vlambda-0.2.4}/vcraft_build.py +0 -0
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"""vlambda: an ASGI-like server for Python lambdas, written in V."""
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
|
|
5
|
+
# Sandboxed runners only need the pure-Python modules; importing the native
|
|
6
|
+
# extension there adds startup latency without being used.
|
|
7
|
+
if os.environ.get("VLRUN_NO_NATIVE") != "1":
|
|
8
|
+
from vlambda_native import * # noqa: F401,F403
|
|
@@ -113,7 +113,9 @@ def main() -> None:
|
|
|
113
113
|
body = body or b""
|
|
114
114
|
# Response cache for GET requests, storing the final serialized payload.
|
|
115
115
|
cache_key = None
|
|
116
|
-
|
|
116
|
+
# A lambda that writes logs has side effects; never serve it from the
|
|
117
|
+
# GET response cache or its log lines would silently disappear.
|
|
118
|
+
if method == "GET" and "request.log" not in code:
|
|
117
119
|
cache_key = f"{code}:{path}:{query}"
|
|
118
120
|
from vlambda.cache import get_response
|
|
119
121
|
cached = get_response(cache_key)
|
|
@@ -399,11 +401,11 @@ def main() -> None:
|
|
|
399
401
|
|
|
400
402
|
from vlambda.project import create_project as _create_project, execute_project as _execute_project, delete_project as _delete_project_impl, schedule_delete as _schedule_delete_impl
|
|
401
403
|
|
|
402
|
-
def _run_project(project_dir, entrypoint, method, path, query, body):
|
|
404
|
+
def _run_project(project_dir, entrypoint, method, path, query, body, request_id, slug):
|
|
403
405
|
import json
|
|
404
406
|
timeout = args.invoke_timeout
|
|
405
407
|
try:
|
|
406
|
-
result = pool.execute_project(project_dir, entrypoint, method, path, query, body, timeout=timeout)
|
|
408
|
+
result = pool.execute_project(project_dir, entrypoint, method, path, query, body, timeout=timeout, request_id=request_id, slug=slug)
|
|
407
409
|
if isinstance(result, str):
|
|
408
410
|
return result
|
|
409
411
|
if isinstance(result, (dict, list)):
|
|
@@ -280,9 +280,32 @@ def _runner_env() -> dict[str, str]:
|
|
|
280
280
|
env["HOME"] = "/tmp"
|
|
281
281
|
env["TMPDIR"] = "/tmp"
|
|
282
282
|
env["PYTHONDONTWRITEBYTECODE"] = "1"
|
|
283
|
+
env["VLRUN_NO_NATIVE"] = "1"
|
|
283
284
|
return env
|
|
284
285
|
|
|
285
286
|
|
|
287
|
+
def _record_runner_logs(logs) -> None:
|
|
288
|
+
"""Persist logs produced in a short-lived runner process."""
|
|
289
|
+
if not isinstance(logs, list) or not logs:
|
|
290
|
+
return
|
|
291
|
+
try:
|
|
292
|
+
from vlambda.metrics import record_log
|
|
293
|
+
except Exception:
|
|
294
|
+
return
|
|
295
|
+
for item in logs:
|
|
296
|
+
if not isinstance(item, dict):
|
|
297
|
+
continue
|
|
298
|
+
try:
|
|
299
|
+
record_log(
|
|
300
|
+
str(item.get("request_id", "")),
|
|
301
|
+
str(item.get("slug", "")),
|
|
302
|
+
str(item.get("level", "info")),
|
|
303
|
+
str(item.get("message", "")),
|
|
304
|
+
)
|
|
305
|
+
except Exception:
|
|
306
|
+
pass
|
|
307
|
+
|
|
308
|
+
|
|
286
309
|
class ProcessWorker:
|
|
287
310
|
def __init__(self):
|
|
288
311
|
self._lock = threading.Lock()
|
|
@@ -360,6 +383,7 @@ class ProcessWorker:
|
|
|
360
383
|
except json.JSONDecodeError:
|
|
361
384
|
self._proc = None
|
|
362
385
|
return {"error": "invalid runner response"}
|
|
386
|
+
_record_runner_logs(envelope.get("logs"))
|
|
363
387
|
if envelope.get("ok"):
|
|
364
388
|
return envelope.get("payload")
|
|
365
389
|
return {"error": envelope.get("error", "execution failed")}
|
|
@@ -505,6 +529,7 @@ class LandlockWorker:
|
|
|
505
529
|
envelope = json.loads(lines[-1])
|
|
506
530
|
except json.JSONDecodeError:
|
|
507
531
|
return {"error": "invalid runner response"}
|
|
532
|
+
_record_runner_logs(envelope.get("logs"))
|
|
508
533
|
if envelope.get("ok"):
|
|
509
534
|
return envelope.get("payload")
|
|
510
535
|
return {"error": envelope.get("error", "execution failed")}
|
|
@@ -608,6 +633,7 @@ class NsjailWorker:
|
|
|
608
633
|
envelope = json.loads(lines[-1])
|
|
609
634
|
except json.JSONDecodeError:
|
|
610
635
|
return {"error": "invalid runner response"}
|
|
636
|
+
_record_runner_logs(envelope.get("logs"))
|
|
611
637
|
if envelope.get("ok"):
|
|
612
638
|
return envelope.get("payload")
|
|
613
639
|
return {"error": envelope.get("error", "execution failed")}
|
|
@@ -2,6 +2,19 @@
|
|
|
2
2
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
|
+
_log_sink = None
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def set_log_sink(sink):
|
|
9
|
+
"""Route Request.log() to an alternate sink (used by sandboxed runners)."""
|
|
10
|
+
global _log_sink
|
|
11
|
+
_log_sink = sink
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def clear_log_sink():
|
|
15
|
+
global _log_sink
|
|
16
|
+
_log_sink = None
|
|
17
|
+
|
|
5
18
|
|
|
6
19
|
class Request:
|
|
7
20
|
__slots__ = ("method", "path", "query_string", "headers", "body", "request_id", "slug")
|
|
@@ -18,6 +31,9 @@ class Request:
|
|
|
18
31
|
|
|
19
32
|
def log(self, message, level="info"):
|
|
20
33
|
"""Persist a log line for this invocation."""
|
|
34
|
+
if _log_sink is not None:
|
|
35
|
+
_log_sink(self.request_id, self.slug, level, message)
|
|
36
|
+
return None
|
|
21
37
|
from vlambda.metrics import record_log
|
|
22
38
|
record_log(self.request_id, self.slug, level, message)
|
|
23
39
|
return None
|
|
@@ -17,9 +17,23 @@ if ROOT not in sys.path:
|
|
|
17
17
|
sys.path.insert(0, ROOT)
|
|
18
18
|
|
|
19
19
|
from vlambda.cache import get as get_compiled
|
|
20
|
-
from vlambda.request import Request
|
|
20
|
+
from vlambda.request import Request, set_log_sink
|
|
21
21
|
from vlambda.sandbox import make_sandbox
|
|
22
22
|
|
|
23
|
+
_current_logs: list[dict] = []
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def _log_sink(request_id: str, slug: str, level: str, message: str):
|
|
27
|
+
_current_logs.append({
|
|
28
|
+
"request_id": request_id,
|
|
29
|
+
"slug": slug,
|
|
30
|
+
"level": level,
|
|
31
|
+
"message": str(message),
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
set_log_sink(_log_sink)
|
|
36
|
+
|
|
23
37
|
|
|
24
38
|
def _execute(code: str, method: str, path: str, query: str, body: bytes, request_id: str = "", slug: str = ""):
|
|
25
39
|
compiled, ns_template, handler = get_compiled(code)
|
|
@@ -79,6 +93,7 @@ def main() -> None:
|
|
|
79
93
|
line = line.strip()
|
|
80
94
|
if not line:
|
|
81
95
|
continue
|
|
96
|
+
_current_logs.clear()
|
|
82
97
|
try:
|
|
83
98
|
req = json.loads(line)
|
|
84
99
|
code = req.get("code", "")
|
|
@@ -113,9 +128,9 @@ def main() -> None:
|
|
|
113
128
|
payload = _json.dumps(result, default=_default)
|
|
114
129
|
else:
|
|
115
130
|
payload = str(result)
|
|
116
|
-
print(json.dumps({"ok": True, "payload": payload}), flush=True)
|
|
131
|
+
print(json.dumps({"ok": True, "payload": payload, "logs": _current_logs}), flush=True)
|
|
117
132
|
except Exception as e:
|
|
118
|
-
print(json.dumps({"ok": False, "error": f"{type(e).__name__}: {e}"}), flush=True)
|
|
133
|
+
print(json.dumps({"ok": False, "error": f"{type(e).__name__}: {e}", "logs": _current_logs}), flush=True)
|
|
119
134
|
|
|
120
135
|
|
|
121
136
|
if __name__ == "__main__":
|
|
@@ -32,21 +32,9 @@ def blocked_import(name, *args, **kwargs):
|
|
|
32
32
|
|
|
33
33
|
|
|
34
34
|
# Pre-imported modules (loaded once, shared across sandboxes)
|
|
35
|
+
# Modules are imported on demand by blocked_import. Preloading them at module
|
|
36
|
+
# import makes every fresh sandbox process pay for modules the lambda may not use.
|
|
35
37
|
_PREIMPORTED = {}
|
|
36
|
-
def _preload():
|
|
37
|
-
import json, math, re, datetime, collections, itertools, functools
|
|
38
|
-
import hashlib, base64, string, textwrap, operator
|
|
39
|
-
import decimal, fractions, random, statistics
|
|
40
|
-
_PREIMPORTED.update({
|
|
41
|
-
"json": json, "math": math, "re": re,
|
|
42
|
-
"datetime": datetime, "collections": collections,
|
|
43
|
-
"itertools": itertools, "functools": functools,
|
|
44
|
-
"hashlib": hashlib, "base64": base64, "string": string,
|
|
45
|
-
"textwrap": textwrap, "operator": operator,
|
|
46
|
-
"decimal": decimal, "fractions": fractions,
|
|
47
|
-
"random": random, "statistics": statistics,
|
|
48
|
-
})
|
|
49
|
-
_preload()
|
|
50
38
|
|
|
51
39
|
|
|
52
40
|
|
|
@@ -1566,7 +1566,9 @@ pub fn (mut h VlambdaHandler) handle(req http.Request) http.Response {
|
|
|
1566
1566
|
if qi2 := req.url.index('?') {
|
|
1567
1567
|
q = req.url[qi2+1..]
|
|
1568
1568
|
}
|
|
1569
|
-
|
|
1569
|
+
vlambda_request_counter++
|
|
1570
|
+
request_id := 'req-${vlambda_request_counter}'
|
|
1571
|
+
result := run_project(project_dir, entrypoint, method, req.url, q, req.data, request_id, slug)
|
|
1570
1572
|
elapsed := time.since(start_time).microseconds()
|
|
1571
1573
|
vlambda_latency_sum += f64(elapsed) / 1000.0
|
|
1572
1574
|
vlambda_latency_count++
|
|
@@ -1575,7 +1577,9 @@ pub fn (mut h VlambdaHandler) handle(req http.Request) http.Response {
|
|
|
1575
1577
|
if result.contains('error') {
|
|
1576
1578
|
vlambda_lambda_errors[slug] = (vlambda_lambda_errors[slug] or { u64(0) }) + 1
|
|
1577
1579
|
}
|
|
1578
|
-
|
|
1580
|
+
mut resp := cors_response(200, result, lambda_content_type(result))
|
|
1581
|
+
resp.header.add_custom('X-Request-Id', request_id) or {}
|
|
1582
|
+
return resp
|
|
1579
1583
|
}
|
|
1580
1584
|
// Split code and method
|
|
1581
1585
|
parts := code_method.split('|')
|
|
@@ -2448,14 +2452,14 @@ pub fn run_lambda(request_id string, slug string, code string, method string, pa
|
|
|
2448
2452
|
}
|
|
2449
2453
|
|
|
2450
2454
|
@[vc_fn]
|
|
2451
|
-
pub fn run_project(project_dir string, entrypoint string, method string, path string, query string, body string) string {
|
|
2455
|
+
pub fn run_project(project_dir string, entrypoint string, method string, path string, query string, body string, request_id string, slug string) string {
|
|
2452
2456
|
vlambda_result_str = '{"error": "not started"}'
|
|
2453
|
-
vcraft.with_gil(fn [project_dir, entrypoint, method, path, query, body] () {
|
|
2457
|
+
vcraft.with_gil(fn [project_dir, entrypoint, method, path, query, body, request_id, slug] () {
|
|
2454
2458
|
if vlambda_run_project == unsafe { nil } {
|
|
2455
2459
|
vlambda_result_str = '{"error": "no project runner registered"}'
|
|
2456
2460
|
return
|
|
2457
2461
|
}
|
|
2458
|
-
args := [vcraft.to_py_string(project_dir).ptr, vcraft.to_py_string(entrypoint).ptr, vcraft.to_py_string(method).ptr, vcraft.to_py_string(path).ptr, vcraft.to_py_string(query).ptr, body_to_py_bytes(body).ptr]
|
|
2462
|
+
args := [vcraft.to_py_string(project_dir).ptr, vcraft.to_py_string(entrypoint).ptr, vcraft.to_py_string(method).ptr, vcraft.to_py_string(path).ptr, vcraft.to_py_string(query).ptr, body_to_py_bytes(body).ptr, vcraft.to_py_string(request_id).ptr, vcraft.to_py_string(slug).ptr]
|
|
2459
2463
|
result := vcraft.call(vlambda_run_project, args)
|
|
2460
2464
|
for a in args { vcraft.borrow(a).decref() }
|
|
2461
2465
|
if result.is_null() {
|
|
@@ -385,6 +385,7 @@ def test_landlock_sandbox():
|
|
|
385
385
|
" check('fork', lambda: os.fork())\n"
|
|
386
386
|
" check('subprocess', lambda: __import__('subprocess').run(['/bin/true']))\n"
|
|
387
387
|
" check('tmp_other', lambda: open('/tmp/not-mine', 'w'))\n"
|
|
388
|
+
" request.log('landlock log line')\n"
|
|
388
389
|
" values = []\n"
|
|
389
390
|
" thread = threading.Thread(target=lambda: values.append(1))\n"
|
|
390
391
|
" thread.start(); thread.join()\n"
|
|
@@ -405,6 +406,15 @@ def test_landlock_sandbox():
|
|
|
405
406
|
assert body.get("tmp_other") == "PermissionError", body
|
|
406
407
|
assert body.get("thread") == [1], body
|
|
407
408
|
assert body.get("env") == [], body
|
|
409
|
+
deadline = time.time() + 4
|
|
410
|
+
logs = {}
|
|
411
|
+
while time.time() < deadline:
|
|
412
|
+
status, logs = req("GET", f"/logs/history?days=1&slug={slug}", token=token)
|
|
413
|
+
assert status == 200
|
|
414
|
+
if any(item.get("message") == "landlock log line" for item in logs.get("items", [])):
|
|
415
|
+
break
|
|
416
|
+
time.sleep(0.3)
|
|
417
|
+
assert any(item.get("message") == "landlock log line" for item in logs.get("items", [])), logs
|
|
408
418
|
finally:
|
|
409
419
|
stop_server()
|
|
410
420
|
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|