-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathserver.py
More file actions
1581 lines (1387 loc) · 66.8 KB
/
Copy pathserver.py
File metadata and controls
1581 lines (1387 loc) · 66.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""Tofu Server — Quart + Hypercorn (ASGI).
App entry point. Uses:
- Quart (async Flask from Pallets) as the application framework
- Hypercorn as the ASGI server with HTTP/2 support
- Optional TLS/HTTP2 for explicitly configured direct deployments
All existing Flask-style sync route handlers run unchanged in a thread pool.
Usage:
python server.py # HTTP/1.1 (proxy-safe default)
TOFU_TLS=1 python server.py # HTTPS + HTTP/2 (auto-cert)
python server.py --no-tls # Explicit HTTP/1.1
python server.py --certfile cert.pem --keyfile key.pem # custom cert
"""
import asyncio
import os
import sys
import json
import logging
import time
import threading
import faulthandler
from runtime_guards import (
RESOURCE_BUDGET_ENV_KEYS,
deployment_resource_default,
install_runtime_resource_defaults,
)
def _delegate_executable_to_manager():
"""Fast-path a human ``python server.py`` into the sole lifecycle owner."""
external_owner = (
os.environ.get('TOFU_SERVER_WORKER') == '1'
or os.environ.get('_TOFU_VIA_BOOTSTRAP') == '1'
or os.environ.get('TOFU_RUN_SERVER') == '1'
# In-place update/HEAD re-exec keeps the original worker PID and sets
# this port handoff marker before execv. It is already the worker; a
# manager handoff here would strand that PID holding the instance lock
# while it waits for itself to become ready.
or bool(os.environ.get('_TOFU_REEXEC_PORT'))
or os.getpid() == 1 # Docker/container entrypoint owns this process
)
if __name__ != '__main__' or external_owner:
return
if '--version' in sys.argv[1:]:
try:
with open(os.path.join(os.path.dirname(__file__), 'VERSION'),
encoding='utf-8') as version_file:
version = version_file.read().strip() or 'unknown'
except OSError:
version = 'unknown'
sys.stdout.write(f'Tofu {version}\n')
raise SystemExit(0)
if any(arg in ('-h', '--help') for arg in sys.argv[1:]):
sys.stdout.write(
'usage: python server.py [--host HOST] [--port PORT] [--no-tls] '
'[--certfile FILE --keyfile FILE]\n\n'
'Starts Tofu through the project-local manager.\n'
'Lifecycle operations: python serverctl.py --help\n'
'Start diagnostics : python serverctl.py doctor\n')
raise SystemExit(0)
# Preserve the established environment-selection contract without loading
# the application first. The re-executed wrapper will immediately hand off
# to serverctl; the eventual worker still runs the full native-path setup.
project = os.path.dirname(os.path.abspath(__file__))
marker = os.path.join(project, '.tofu_env.json')
try:
with open(marker, encoding='utf-8') as fh:
cfg = json.load(fh)
target = cfg.get('python') or ''
prefix = cfg.get('env_prefix') or ''
in_target = (
os.path.realpath(sys.prefix) == os.path.realpath(prefix)
if prefix else os.path.realpath(sys.executable) == os.path.realpath(target)
)
if target and os.access(target, os.X_OK) and not in_target \
and os.environ.get('_TOFU_ENV_REEXEC') != '1':
os.environ['_TOFU_ENV_REEXEC'] = '1'
os.execv(target, [target, *sys.argv])
except (OSError, ValueError, TypeError):
pass
try:
from serverctl import managed_start
code = managed_start(sys.argv[1:], wait=180.0, source='python-server.py')
except Exception as exc:
sys.stderr.write(
'[server.py] Could not hand startup to the Tofu manager: %s\n'
'Diagnose with: %s serverctl.py doctor\n' % (exc, sys.executable))
code = 1
raise SystemExit(code)
_delegate_executable_to_manager()
def _load_early_resource_environment() -> None:
"""Expose project resource/data knobs before native pools are imported."""
if __name__ != '__main__':
return
from tofu_dotenv import read_dotenv_values
project = os.path.dirname(os.path.abspath(__file__))
os.environ.setdefault('TOFU_PROJECT_PATH', project)
try:
values = read_dotenv_values(os.path.join(project, '.env'))
except OSError:
# The canonical loader below emits the established actionable startup
# error. Early policy installation must not replace it with a traceback.
return
early_names = RESOURCE_BUDGET_ENV_KEYS | {
'TOFU_DEPLOYMENT_MODE',
'TOFU_DATA_DIR',
'TOFU_DATA_LAYOUT',
'XDG_DATA_HOME',
'LOCALAPPDATA',
}
for name in early_names:
if name not in os.environ and name in values:
os.environ[name] = values[name]
_load_early_resource_environment()
# Docker runs server.py as PID 1 and therefore has no Python parent that can
# materialize the adaptive personal budget. Freeze one probe before importing
# NumPy, routes, pools, or storage so every consumer (and the later Sidecar
# child) sees the same values for this boot.
_RESOURCE_BUDGET = (
install_runtime_resource_defaults(os.environ)
if __name__ == '__main__' else None)
def _install_numeric_thread_defaults() -> int:
"""Bound implicit BLAS/OpenMP pools before NumPy or ML imports.
High-core personal hosts otherwise make OpenBLAS eagerly retain one native
worker per visible CPU (64 in the measured deployment) even while Tofu is
idle. Tofu already owns request, DB, agent, and tool executors; an
additional host-sized pool per numeric runtime causes oversubscription and
needless thread stacks under memory pressure. ``TOFU_NUMERIC_THREADS`` is
the process-wide ceiling; a smaller library-specific value remains valid,
while a larger inherited host setting is clamped before imports.
"""
default = deployment_resource_default(
'TOFU_NUMERIC_THREADS', os.environ)
raw = os.environ.get('TOFU_NUMERIC_THREADS', str(default))
try:
workers = int(raw or default)
except (TypeError, ValueError):
workers = default
workers = max(1, min(32, workers))
for name in ('OPENBLAS_NUM_THREADS', 'OMP_NUM_THREADS',
'MKL_NUM_THREADS', 'NUMEXPR_NUM_THREADS'):
try:
library_workers = int(os.environ.get(name, '') or workers)
except (TypeError, ValueError, OverflowError):
library_workers = workers
os.environ[name] = str(max(1, min(workers, library_workers)))
return workers
# Must run before the first ``lib`` import: route/plugin discovery eventually
# imports NumPy, at which point OpenBLAS has already fixed its native pool.
_NUMERIC_THREADS = _install_numeric_thread_defaults()
# ── Capture C-level fatal signals (SIGSEGV / SIGABRT / SIGFPE / SIGILL / SIGBUS) ──
# These fire on heap corruption (e.g. `munmap_chunk(): invalid pointer`) from
# native extensions like urllib3's response decompressor. Without this the
# abort prints to fd 2 only and we lose the Python stack of every thread.
# Writing to a dedicated file (instead of stderr) ensures the trace survives
# even when stderr is the controlling terminal of a process that's about
# to die. all_threads=True captures every Python thread, not just the
# crashing one — essential for diagnosing concurrent-fetch races.
#
# Dual-sink strategy: write to BOTH a per-process FUSE-backed file (durable
# across box restarts) and a per-process tmpfs mirror (immune to FUSE stalls).
# Only a real ``python server.py`` process arms the sinks. Historically this
# ran on every ``import server`` and appended to one shared file: test/tool
# imports alone produced 44k headers, while 1,598 stall dumps grew it to
# 125 MiB with no bound.
_fault_log = None
_fault_shm_log = None
_FAULT_LOG_DIR = os.path.join(
os.path.dirname(os.path.abspath(__file__)), 'logs')
_FAULT_LOG_PATH = os.path.join(
_FAULT_LOG_DIR, 'tofu_faulthandler_%d.log' % os.getpid())
_FAULT_SHM_PATH = '/dev/shm/tofu_faulthandler_%d.log' % os.getpid()
def _arm_faulthandler_sinks():
"""Open early crash sinks for the executable server, never an importer."""
global _fault_log, _fault_shm_log, _FAULT_LOG_DIR, _FAULT_LOG_PATH
try:
from lib.log_policy import LOG_FILE_MODE as _fault_file_mode
except Exception:
_fault_file_mode = 0o600
header = '=== faulthandler armed pid=%d at %s ===\n' % (
os.getpid(), time.strftime('%Y-%m-%d %H:%M:%S'))
# Arm tmpfs first, before importing even the lightweight writable-path
# resolver. This preserves early native-crash capture while ensuring a
# fresh/XDG or frozen install places its durable file beside all other logs
# instead of writing into a source/read-only bundle.
try:
_fault_shm_log = open(_FAULT_SHM_PATH, 'w+', buffering=1)
try:
os.fchmod(_fault_shm_log.fileno(), _fault_file_mode)
except (AttributeError, OSError):
os.chmod(_FAULT_SHM_PATH, _fault_file_mode)
_fault_shm_log.write(header)
faulthandler.enable(file=_fault_shm_log, all_threads=True)
except (OSError, RuntimeError):
if _fault_shm_log is not None:
try:
_fault_shm_log.close()
except OSError:
pass
_fault_shm_log = None
try:
from lib.log import LOG_DIR as _writable_log_dir
_FAULT_LOG_DIR = _writable_log_dir
_FAULT_LOG_PATH = os.path.join(
_FAULT_LOG_DIR, 'tofu_faulthandler_%d.log' % os.getpid())
except Exception:
pass
try:
os.makedirs(_FAULT_LOG_DIR, exist_ok=True)
_fault_log = open(_FAULT_LOG_PATH, 'w+', buffering=1)
try:
os.fchmod(_fault_log.fileno(), _fault_file_mode)
except (AttributeError, OSError):
os.chmod(_FAULT_LOG_PATH, _fault_file_mode)
_fault_log.write(header)
except OSError:
_fault_log = None
# Fall back to the durable fd, then stderr, when tmpfs is unavailable.
# ``w+`` lets the healthy heartbeat cap repeated non-fatal stall dumps
# without replacing the descriptor retained by faulthandler.
if _fault_shm_log is None:
if _fault_log is not None:
faulthandler.enable(file=_fault_log, all_threads=True)
else:
faulthandler.enable(all_threads=True)
if __name__ == '__main__':
_arm_faulthandler_sinks()
# ── Single-instance startup lock + heartbeat wedge detection ──
# (moved to lib/server_boot/lock.py; re-exported for asgi.py and tests.)
from lib.server_boot.lock import ( # noqa: F401
_acquire_instance_lock,
_boot_heartbeat_stale_threshold,
_heartbeat_dir,
_heartbeat_path,
_heartbeat_stale_threshold,
_holder_wedge_age,
_pid_alive,
_pid_is_live_server,
_read_heartbeat,
_read_heartbeat_state,
_read_instance_lock_entry,
_reclaim_stale_instance_lock,
_record_serve_mode,
_serve_mode_path,
_write_heartbeat,
)
# ── Faulthandler-sink hygiene + event-loop stall detection (pure helpers) ──
# The pure helpers live in lib/server_fault_watchdog.py and are re-exported
# here so ``server.<name>`` / ``from server import <name>`` keep working.
from lib.server_fault_watchdog import ( # noqa: F401
_extract_loop_top_frame,
_fault_dump_limits,
_loop_stall_decide,
_parse_fault_dump_pid,
_prune_fault_dump_budget,
_prune_stale_fault_dumps,
_reset_fault_sink,
_should_arm_ctimer,
_stall_pressure_context,
_trim_fault_sink_if_oversize,
)
def _port_bound(port, host='127.0.0.1', timeout=0.5):
"""True iff a TCP connection to host:port succeeds (listener present).
Scheme-agnostic: works for TLS and plain-HTTP listeners alike."""
import socket as _s
try:
with _s.create_connection((host, port), timeout=timeout):
return True
except OSError:
return False
def _listener_death_decide(was_bound, bound, misses, k):
"""Pure decision for the serve-listener watch (second layer after the
loop heartbeat). Returns ``(was_bound, misses, should_exit)``.
Arms only after the listener was seen bound at least once (the pre-serve
startup window is not our watch); counts CONSECUTIVE misses from there;
a single recovery resets the streak. The serve task dying while the
loop stays alive (the 2026-08-03 11:14 state: no listener, live lock,
FRESH heartbeat) is invisible to every external probe — the watchdog
sees a live pid and yields forever — so the process must die loudly
itself and hand the watchdog a clean, handleable death."""
if bound:
return (True, 0, False)
if not was_bound:
return (False, 0, False)
misses += 1
return (True, misses, misses >= k)
# One-shot boot cleanup: retain recent dead-process evidence but bound both the
# tmpfs and durable per-pid families. The file opened for this process and any
# other genuinely live server are always preserved.
if _fault_shm_log is not None:
try:
_fault_limits = _fault_dump_limits()
_pruned = _prune_fault_dump_budget(
directory='/dev/shm',
keep_basename=os.path.basename(_FAULT_SHM_PATH),
max_dead_files=_fault_limits['stale_files'],
max_dead_bytes=_fault_limits['stale_bytes'])
if _pruned:
sys.stderr.write('[boot] pruned %d over-budget faulthandler dump(s) from /dev/shm\n'
% _pruned)
except Exception:
pass # cleanup is best-effort; never block boot on it
if _fault_log is not None:
try:
_fault_limits = _fault_dump_limits()
_pruned = _prune_fault_dump_budget(
directory=_FAULT_LOG_DIR,
keep_basename=os.path.basename(_FAULT_LOG_PATH),
max_dead_files=_fault_limits['stale_files'],
max_dead_bytes=_fault_limits['stale_bytes'])
if _pruned:
sys.stderr.write('[boot] pruned %d over-budget durable fault dump(s)\n'
% _pruned)
except Exception:
pass
# ── Pin mapped pages into RAM (FUSE SIGBUS mitigation) ──
# The FUSE/cgroup headroom gate lives in lib/server_mlock.py; this module
# keeps the import-time decision + mlockall exec. Production default is
# OFF (TOFU_MLOCK=1 forces it on, =auto enables the legacy headroom-gated
# mode).
from lib.server_mlock import ( # noqa: F401
_tofu_cgroup_mem_limit_bytes,
_tofu_cgroup_mem_usage_bytes,
_tofu_path_is_fuse,
_tofu_should_mlock,
)
_tofu_do_mlock, _tofu_mlock_reason = _tofu_should_mlock()
if _tofu_do_mlock:
try:
import ctypes as _ctypes
_MCL_CURRENT, _MCL_FUTURE = 1, 2
_libc = _ctypes.CDLL('libc.so.6', use_errno=True)
if _libc.mlockall(_MCL_CURRENT | _MCL_FUTURE) != 0:
import errno as _errno
_mlk_err = _ctypes.get_errno()
# ENOMEM (12) = memlock rlimit too low — common in containers
if _mlk_err == _errno.ENOMEM:
os.write(2, b'[boot] mlockall skipped: memlock rlimit too low\n')
else:
os.write(2, (b'[boot] mlockall failed errno=%d\n' % _mlk_err))
else:
os.write(2, b'[boot] mlockall(MCL_CURRENT|MCL_FUTURE) OK '
b'\xe2\x80\x94 pages pinned\n')
except Exception as _mlk_exc:
try:
os.write(2, (b'[boot] mlockall unavailable: %s\n'
% str(_mlk_exc).encode(errors='replace')))
except OSError:
pass
else:
try:
os.write(2, (b'[boot] mlockall skipped \xe2\x80\x94 %s\n'
% _tofu_mlock_reason.encode(errors='replace')))
except OSError:
pass
# ── Record process start time (same as server.py) ──
_PROC_T0 = time.time()
try:
os.write(2, b'\033[36m[boot + 0.0s]\033[0m \xf0\x9f\xab\xa7 Tofu '
b'async bootstrap \xe2\x80\x94 importing core libraries\xe2\x80\xa6\n')
except OSError:
pass
# ── Native-linkage forensics: which libstdc++ owns the soname? ──
# The capture body lives in lib/server_linkage_forensics.py; the
# diagnostic line and exception guard stay here (source pin in
# tests/test_startup_stdcxx_forensics.py).
from lib.server_linkage_forensics import capture_linkage_forensics
try:
_TOFU_LINKAGE_FORENSICS = capture_linkage_forensics()
except Exception:
_TOFU_LINKAGE_FORENSICS = 'libstdc++ soname -> unavailable'
try:
os.write(2, ('[boot] %s\n' % _TOFU_LINKAGE_FORENSICS).encode(errors='replace'))
except OSError:
pass
# ── Auto-activate conda env (reuse server.py logic) ──
# This must happen before any third-party imports.
_PROJ_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, _PROJ_DIR)
# ── Dev fallback: prefer the monorepo's canonical tofu-search source when it
# is not pip-installed (production installs it via requirements.txt). An
# explicit TOFU_SEARCH_PATH remains available for packaged/source-export tests.
_TOFU_SEARCH_PATH = os.environ.get('TOFU_SEARCH_PATH', '').strip()
if not _TOFU_SEARCH_PATH:
_TOFU_SEARCH_PATH = os.path.join(_PROJ_DIR, 'packages', 'tofu-search')
if _TOFU_SEARCH_PATH and os.path.isdir(_TOFU_SEARCH_PATH):
sys.path.insert(0, _TOFU_SEARCH_PATH)
def _tofu_export_env_native_paths(env_prefix, backend):
"""Put the env's lib/ + bin/ on the search paths for CHILD processes.
The headless-Chromium half (LD_LIBRARY_PATH + fontconfig) is delegated to
chromium_env.ensure_chromium_env() — the single source of truth shared with
bootstrap.py, tests/conftest.py and lib/motion_video. It resolves from
sys.prefix, so it works even with no .tofu_env.json marker (a fresh clone,
an exported bundle, or Docker), which is precisely the case the four
hand-copied versions used to miss. See chromium_env.py's docstring.
What stays here is the marker-specific part: PATH and the CONDA_* shims.
"""
# Chromium's GUI libs + fonts. Passing env_prefix only ADDS a candidate;
# resolution still works when the marker is absent or wrong.
try:
from chromium_env import ensure_chromium_env
ensure_chromium_env(env_prefix=env_prefix)
except Exception as _e:
sys.stderr.write(f'[server.py] chromium env setup skipped: {_e}\n')
if not env_prefix or not os.path.isdir(env_prefix):
return
env_bin = os.path.join(env_prefix, 'bin')
if os.path.isdir(env_bin):
_cur = os.environ.get('PATH', '')
if env_bin not in _cur.split(os.pathsep):
os.environ['PATH'] = (env_bin + os.pathsep + _cur) if _cur else env_bin
# Only masquerade as a conda env when we ARE one. A uv venv
# (backend='uv') is not conda; setting CONDA_PREFIX would make
# bootstrap.py's _running_in_conda_env() misfire and route its pip
# fallback down the conda-forge branch.
if backend != 'uv':
os.environ.setdefault('CONDA_PREFIX', env_prefix)
def _tofu_maybe_reexec_into_env():
"""Re-exec into Tofu's conda env if not already there."""
marker = os.path.join(_PROJ_DIR, '.tofu_env.json')
if not os.path.isfile(marker):
# No marker (fresh clone, exported bundle, Docker, pip layout) — there
# is nothing to re-exec into, but Chromium still needs its GUI libs and
# fonts on the search paths. chromium_env resolves those from
# sys.prefix, so this path is no longer a dead browser.
_tofu_export_env_native_paths('', '')
return
try:
with open(marker, 'r', encoding='utf-8') as f:
cfg = json.load(f)
except Exception:
return
target_py = cfg.get('python') or ''
env_prefix = cfg.get('env_prefix') or ''
backend = cfg.get('backend') or ''
if not target_py or not os.access(target_py, os.X_OK):
return
# Are we ALREADY running inside the target env? Prefer a prefix check over a
# bare interpreter-path comparison: a uv venv's bin/python is a symlink to a
# base CPython, so realpath(target_py) can equal realpath(sys.executable)
# even though we are NOT running with the venv's site-packages active —
# comparing sys.prefix to env_prefix catches that. Fall back to the
# interpreter-path compare when env_prefix is absent.
already_in_env = False
if env_prefix:
try:
already_in_env = (os.path.realpath(sys.prefix) == os.path.realpath(env_prefix))
except OSError:
already_in_env = (sys.prefix == env_prefix)
else:
try:
already_in_env = os.path.realpath(target_py) == os.path.realpath(sys.executable)
except OSError:
already_in_env = (target_py == sys.executable)
# Export the env's native-library search path BEFORE the already_in_env
# early return. Chromium is a CHILD process and resolves libatk /
# libatk-bridge / libnss out of $env_prefix/lib, which is not on the
# default linker path. `python server.py` launched directly with the env
# interpreter (the documented way, and how the supervisor runs it) takes
# that early return, so while this lived inside the re-exec branch the
# variables were only ever set on the path that re-execs — a directly
# launched server left them unset and every Playwright launch died with
# "libatk-1.0.so.0: cannot open shared object file".
_tofu_export_env_native_paths(env_prefix, backend)
if already_in_env:
return
if os.environ.get('_TOFU_ENV_REEXEC') == '1':
return
os.environ['_TOFU_ENV_REEXEC'] = '1'
try:
os.execv(target_py, [target_py, *sys.argv])
except OSError:
os.environ.pop('_TOFU_ENV_REEXEC', None)
_tofu_maybe_reexec_into_env()
# ── .env loading ──
def _load_dotenv():
from tofu_dotenv import load_dotenv_file
load_dotenv_file(os.path.join(_PROJ_DIR, '.env'))
try:
_load_dotenv()
except OSError as _dotenv_error:
if __name__ != '__main__':
raise
sys.stderr.write(
f'[server.py] Invalid project .env: {_dotenv_error}\n'
'[server.py] Fix or replace the project .env, then run '
'`python serverctl.py doctor`.\n'
)
raise SystemExit(2) from None
# ═══════════════════════════════════════════════════════════════════════
# Sync→loop boundary helpers (back-compatible test surface)
# ═══════════════════════════════════════════════════════════════════════
# Legacy synchronous routes cross Quart's async request/response boundary via
# ``lib.quart_sync``. Keep these module-level names for existing diagnostics and
# tests without mutating Quart's module or Request class.
def _resolve_sync_body_timeout():
"""Return the configured timeout for an explicit sync boundary."""
from lib.quart_sync import sync_boundary_timeout
return sync_boundary_timeout()
def _await_coro_on_loop(coro, main_loop, timeout):
"""Run an awaitable on ``main_loop`` from an executor thread."""
from lib.quart_sync import await_on_loop
return await_on_loop(coro, main_loop, timeout)
# ═══════════════════════════════════════════════════════════════════════
# Logging (reuse server.py's architecture)
# ═══════════════════════════════════════════════════════════════════════
import mimetypes
mimetypes.init()
mimetypes.add_type('text/javascript', '.js')
mimetypes.add_type('text/javascript', '.mjs')
mimetypes.add_type('text/css', '.css')
mimetypes.add_type('application/json', '.json')
mimetypes.add_type('image/svg+xml', '.svg')
mimetypes.add_type('font/woff2', '.woff2')
mimetypes.add_type('font/ttf', '.ttf')
mimetypes.add_type('application/wasm', '.wasm')
BASE_DIR = _PROJ_DIR
# ── Logging setup (identical to server.py) ──
# LOG_DIR must be WRITABLE. In a frozen desktop build BASE_DIR is the read-only
# bundle root, so route logs to the writable root (see lib/runtime_paths).
from lib.runtime_paths import data_root as _tofu_data_root, logs_root as _tofu_logs_root
from lib.log_retention import (
ensure_private_log_directory as _ensure_private_log_directory,
register_external_log as _register_external_log,
start_log_maintenance as _start_log_maintenance,
stop_log_maintenance as _stop_log_maintenance,
)
LOG_DIR = _tofu_logs_root()
_ensure_private_log_directory(LOG_DIR)
# Acquire process authority before application assembly. This prevents two
# supervised server processes from racing startup work before one eventually
# loses the TCP bind race.
_instance_lock_fd = None
_instance_lock_bypassed = False
_lock_dir = None
_lock_path = None
if __name__ == '__main__':
_lock_dir = _tofu_data_root()
os.makedirs(_lock_dir, exist_ok=True)
_lock_path = os.path.join(_lock_dir, '.server.lock')
_early_lock_log = logging.getLogger('server')
_lock_ok, _instance_lock_fd = _acquire_instance_lock(
_lock_path, _early_lock_log, mark_booting=True)
if not _lock_ok:
_skip = (os.environ.get('TOFU_SKIP_LOCK', '') or '').strip()
if _skip != '1':
_message = (
'Another server instance is already running from this project '
'directory. Set TOFU_SKIP_LOCK=1 to force start.')
_early_lock_log.critical(_message)
try:
sys.stderr.write('[server.py] ERROR: %s\n' % _message)
sys.stderr.flush()
except OSError:
pass
sys.exit(1)
_instance_lock_bypassed = True
_early_lock_log.warning(
'[Lock] TOFU_SKIP_LOCK=1 — bypassing instance lock')
# ── Non-blocking, memory-bounded logging ──
# Filter/handler/queue classes and the one-shot construction live in
# lib/server_logging.py; server.py keeps the lifecycle control functions.
from lib.server_logging import ( # noqa: F401
_BoundedQueueHandler,
_BoundedQueueListener,
_QuietPollFilter,
_SizeAndTimeRotatingFileHandler,
_log_queue_capacity,
build_logging_runtime,
)
# Imported AFTER the instance lock above (the source-order pin in
# tests/test_instance_lock_wedge_reclaim.py).
from lib.log_aggregates import (
enabled as _log_agg_enabled,
get_default_store as _log_agg_store,
start_flusher as _log_agg_start_flusher,
stop_flusher as _log_agg_stop_flusher,
)
# Under pytest, keep logging SYNCHRONOUS: caplog and the tests that assert
# a log line landed in a file handler (e.g. test_log_pytest_sink_isolation)
# read handler output immediately after logger.error(), which an async
# listener thread would race. Detect pytest via the env var it always sets
# for a collected session.
_LOG_UNDER_PYTEST = bool(os.environ.get('PYTEST_CURRENT_TEST')) or (
'pytest' in sys.modules)
(_formatter, _real_log_handlers, _LOG_QUEUE, _queue_handler,
_coalescing_filter, _log_listener) = build_logging_runtime(
log_dir=LOG_DIR,
under_pytest=_LOG_UNDER_PYTEST,
log_agg_enabled=_log_agg_enabled,
log_agg_store=_log_agg_store,
)
# Individual handler handles retained for test/diagnostic introspection
# (tests/test_log_pytest_sink_isolation.py reads these by name).
_app_handler = _real_log_handlers[0]
_access_handler = _real_log_handlers[1]
_error_handler = _real_log_handlers[2]
_vendor_handler = _real_log_handlers[3]
_frontend_handler = _real_log_handlers[4]
_console_handler = _real_log_handlers[5]
def _start_logging_runtime():
"""Start storage-independent log I/O from Quart's serving lifecycle."""
if _LOG_UNDER_PYTEST or _log_listener is None:
return False
thread = getattr(_log_listener, '_thread', None)
if thread is not None and thread.is_alive():
return False
_log_listener.start()
if _coalescing_filter is not None:
# Quiet duplicate tails need one delayed checkpoint even if no later
# event arrives. Arm its sink now; a short-lived worker is created only
# after a delta is actually suppressed. Delivery bypasses the producer
# filter and enters the already-bounded queue directly.
_coalescing_filter.start_pending_flush(_queue_handler.emit)
_external_console = (os.environ.get('TOFU_EXTERNAL_CONSOLE_LOG') or '').strip()
if _external_console:
try:
_external_stream = (
os.environ.get('TOFU_EXTERNAL_CONSOLE_STREAM')
or 'server_console').strip()
_register_external_log(_external_console, _external_stream)
except Exception as exc:
_server_log.warning(
'[Logging] external console retention registration failed: %s',
exc)
_start_log_maintenance(LOG_DIR, _tofu_data_root())
return True
def _start_log_aggregate_runtime_after_recovery():
"""Start the adaptive database-backed log index after recovery settles.
Logging must capture recovery diagnostics, so the listener starts at the
beginning of the Quart lifespan. Its aggregate flusher is different: it
writes through the same single-writer storage authority as recovery and
used to contend with ``turn.recover`` every 15 seconds during startup. The
worker retains that batching delay while rows are pending and sleeps to the
hourly TTL boundary when idle.
"""
if _LOG_UNDER_PYTEST or not _log_agg_enabled():
return False
return _log_agg_start_flusher()
def _stop_logging_runtime(*, timeout=5.0, final_flush=True):
"""Bound and stop aggregate/log threads; safe to call repeatedly."""
if _LOG_UNDER_PYTEST or _log_listener is None:
return True
coalescer_stopped = True
if _coalescing_filter is not None:
coalescer_stopped = _coalescing_filter.stop_pending_flush(
timeout=timeout, final_flush=True)
maintenance_stopped = _stop_log_maintenance(timeout=timeout)
# Drain the listener before the aggregate store's final flush. Otherwise a
# record already in the queue can increment the in-memory fingerprint table
# after its flusher has stopped and disappear from the DB acceleration view.
listener_stopped = _log_listener.stop(timeout=timeout)
aggregate_stopped = True
if _log_agg_enabled():
aggregate_stopped = _log_agg_stop_flusher(
final_flush=final_flush, timeout=timeout)
return bool(coalescer_stopped and maintenance_stopped
and aggregate_stopped and listener_stopped)
if not _LOG_UNDER_PYTEST:
# Drain + flush the queue on interpreter exit so the tail of the log isn't
# lost if the process stops before Quart can run its shutdown lifespan.
import atexit as _atexit_mod
def _stop_log_listener():
try:
_stop_logging_runtime(final_flush=False)
except Exception as exc:
# atexit must not raise; direct stderr is still available if the
# asynchronous listener itself is the failing component.
try:
sys.stderr.write(
'[Server] logging atexit cleanup failed: %s\n' % exc)
except OSError:
pass
_atexit_mod.register(_stop_log_listener)
_NOISY_LIBS = (
'courlan', 'htmldate', 'justext',
'urllib3', 'requests', 'charset_normalizer',
'websockets', 'websockets.client',
'PIL', 'pymupdf',
'httpcore', 'httpx',
)
for _lib_name in _NOISY_LIBS:
logging.getLogger(_lib_name).setLevel(logging.WARNING)
logging.getLogger('trafilatura').setLevel(logging.ERROR)
for _sub in ('trafilatura.xml', 'trafilatura.core', 'trafilatura.htmlprocessing',
'trafilatura.metadata'):
logging.getLogger(_sub).setLevel(logging.ERROR)
logging.getLogger('hypercorn.access').addFilter(_QuietPollFilter())
# ── Crash visibility: route uncaught exceptions to the log files ──
# faulthandler (top of file) covers C-level fatal signals, but an uncaught
# *Python* exception in the main thread otherwise reaches only the default
# excepthook → stderr, never app.log / error.log. Install a hook that logs
# it at CRITICAL (with traceback) before delegating to whatever hook was
# already installed (e.g. the bootstrap-delegation hook that re-execs to
# bootstrap.py on ImportError) — so we add visibility without clobbering it.
_prev_excepthook = sys.excepthook
def _crash_excepthook(exc_type, exc_value, exc_tb):
# Ctrl-C is a normal shutdown path, not a crash — don't scream about it.
if not issubclass(exc_type, KeyboardInterrupt):
try:
# A dynamic-linker failure is unreadable without knowing WHICH copy
# of the library won the soname and what injected it. The boot-time
# forensics went to stderr, which is discarded on any boot the
# watchdog did not start — so attach it to the crash record itself,
# where it lands in logs/error.log next to the traceback.
_extra = ''
if issubclass(exc_type, ImportError):
_msg = str(exc_value)
if 'GLIBCXX' in _msg or 'libstdc++' in _msg or 'symbol' in _msg:
_extra = ' | LINKAGE: %s' % (
globals().get('_TOFU_LINKAGE_FORENSICS', 'unavailable'),)
logging.getLogger('server').critical(
'Uncaught exception — process is terminating%s' % _extra,
exc_info=(exc_type, exc_value, exc_tb))
except Exception:
pass # logging must never mask the original crash
(_prev_excepthook or sys.__excepthook__)(exc_type, exc_value, exc_tb)
sys.excepthook = _crash_excepthook
# ── Crash visibility: background threads ──
# sys.excepthook covers ONLY the main thread. The entire task/orchestration
# system (run_task, swarm agents, scheduler ticks, timers) runs in daemon
# worker threads, where an uncaught exception otherwise reaches just the
# default threading hook → stderr, never app.log / error.log. Route it through
# our 'server' logger at CRITICAL (with traceback) so a silently-dying worker
# is always diagnosable. threading.excepthook exists since Py3.8.
_prev_thread_excepthook = threading.excepthook
def _thread_crash_excepthook(args):
# SystemExit raised inside a thread is a normal stop signal, not a crash.
if not issubclass(args.exc_type, (KeyboardInterrupt, SystemExit)):
try:
logging.getLogger('server').critical(
'Uncaught exception in background thread %r — thread is dying',
getattr(args.thread, 'name', '?'),
exc_info=(args.exc_type, args.exc_value, args.exc_traceback))
except Exception:
pass # logging must never mask the original crash
if _prev_thread_excepthook is not None:
_prev_thread_excepthook(args)
threading.excepthook = _thread_crash_excepthook
# ── Boot progress ──
_BOOT_T0 = _PROC_T0
_boot_logger = logging.getLogger('server.boot')
def _boot(msg, *args):
try:
line = msg % args if args else msg
except Exception:
line = msg
elapsed = time.time() - _BOOT_T0
# The cosmetic console echo must NEVER be fatal. On an in-place restart
# (os.execv) the child inherits fd 2 as a pipe whose reader has already
# gone away, so this write raises BrokenPipeError — which, unguarded,
# kills boot at the very first progress line before any module loads.
# The authoritative boot record is the logger.info below (→ app.log).
try:
sys.stderr.write('\033[36m[boot +%5.1fs]\033[0m %s\n' % (elapsed, line))
sys.stderr.flush()
except OSError:
pass
_boot_logger.info('[boot +%.1fs] %s', elapsed, line)
_boot('🫧 Tofu (async) starting up — loading core modules…')
# ── Keep the unused/broken PyMuPDF layout backend out of ordinary boot ──
# pymupdf4llm auto-activates pymupdf.layout merely because it is installed.
# That backend is incompatible with our RapidOCR version and Tofu explicitly
# uses the classic Markdown implementation, yet activation still imported
# ONNX and retained ~63 native threads + tens of MiB before the first request.
# This stdlib-only policy must run before tofu_search imports pymupdf4llm.
# Structured Docling parsing remains opt-in and installs the ONNX session guard
# at its actual first use.
try:
from runtime_guards import install_pymupdf_classic_policy
install_pymupdf_classic_policy()
except Exception as _pdf_policy_err: # never let an optional policy break boot
_boot('PyMuPDF classic policy install skipped: %s', _pdf_policy_err)
# ── Deployment topology is validated before application imports ──
# Personal is one-process SQLite. Distributed requires external TLS-verified
# PostgreSQL/Redis secret files and an explicit replica identity; removed
# backend/ring switches are fatal instead of selecting a split-brain topology.
from runtime_guards import enforce_deployment_configuration
DEPLOYMENT_CONFIGURATION = enforce_deployment_configuration()
# ═══════════════════════════════════════════════════════════════════════
# Quart App
# ═══════════════════════════════════════════════════════════════════════
# static_folder=None DISABLES Quart's built-in /static/<path> view. That view
# serves files via a NATIVE-ASYNC send_static_file → send_from_directory whose
# is_file()/stat()/full-file-read run DIRECTLY on the event loop — one FUSE
# stall there wedges the whole server (the proven root cause of the outage).
# Our own executor-offloaded /static route below replaces it (see
# _static_route). BASE_DIR/static lives on FUSE here.
from lib.app_factory import create_base_app
app = create_base_app(__name__)
STATIC_DIR = os.path.join(BASE_DIR, 'static')
# Logging handlers are configured early so import/boot records are retained,
# but their worker threads belong to the native application lifespan.
async def _shutdown_logging_runtime():
await asyncio.to_thread(_stop_logging_runtime)
def create_app(config: dict | None = None):
"""Create a fully assembled, independent Quart ASGI application."""
from lib.app_assembly import create_application
return create_application(
__name__,
static_dir=STATIC_DIR,
logger=_lifecycle_log,
secret_key=_load_or_create_flask_secret_key(),
config=config,
body_policy=_HTTP_BODY_POLICY,
static_timeout=lambda: _STATIC_SEND_TIMEOUT,
static_offload=lambda loop, filename: _static_offload(loop, filename),
static_range_allows=lambda value, etag, mtime: _if_range_allows(
value, etag, mtime),
startup_handlers=(
('tofu.logging.startup', _start_logging_runtime),
),
shutdown_handlers=(
('tofu.logging.shutdown', _shutdown_logging_runtime),
),
distributed_preview_read_only=(
DEPLOYMENT_CONFIGURATION.distributed_preview_read_only),
)
def create_production_app(config: dict | None = None):
"""Create an ASGI app with the process-wide production lifespan attached."""
production_app = create_app(config)
register_server_runtime_lifecycle(production_app)
return production_app
# ── Flask secret key (reuse server.py logic) ──
from lib.server_assembly import _load_or_create_flask_secret_key
# MAX_CONTENT_LENGTH is APP-GLOBAL, so it must fit the LARGEST legitimate
# body any route accepts — the video upload cap (512 MiB, TOFU_VIDEO_MAX_BYTES)
# plus multipart slack. Every OTHER route keeps the legacy 50 MiB ceiling via
# the per-route guard below — raising the global must not silently open
# big-body uploads on the whole API surface (owner ruling 2026-08-04).
# ── Long-lived response timeout + bounded request-body policy ──
# Policy, parsing and the Quart before-request hook are owned by the native
# assembly boundary; this module retains only the configured policy value.
from lib.http_body_policy import build_http_body_policy
_HTTP_BODY_POLICY = build_http_body_policy()
_HTTP_BODY_TIMEOUT_S = _HTTP_BODY_POLICY.body_timeout
_HTTP_UPLOAD_BODY_TIMEOUT_S = _HTTP_BODY_POLICY.upload_body_timeout
from lib.log import get_logger
_lifecycle_log = get_logger('server.lifecycle')
# tofu-search is an optional, heavyweight capability. ``lib.search_runtime``
# installs its configuration/browser/auth seams at the first valid search or
# fetch call; ordinary boot and non-search requests never import it.
# ── First-boot personal key bootstrap ──
# Only relevant when the auth gate is in ``private`` or ``multi-user``
# mode. In ``open`` mode (the default for personal installs) no
# credential is required and minting a key would just confuse the
# operator. When in private/multi-user mode and the key store is
# empty, mint a personal admin key
# so the local UI and SDK "just work". The plaintext is printed once
# to stderr and persisted (0600) at data/config/.first_run_token.
# Disable with TOFU_AUTO_KEY=0.
_BOOTSTRAP_TOKEN = ''
try:
from lib.auth_mode import get_mode as _get_auth_mode
_AUTH_MODE = _get_auth_mode()
except Exception as _e:
logging.getLogger('server.boot').warning(
'[AuthMode] could not resolve mode: %s', _e)
_AUTH_MODE = 'open'
def _bootstrap_personal_key_if_needed():
global _BOOTSTRAP_TOKEN
if (os.environ.get('TOFU_AUTO_KEY', '1') or '1').strip() == '0':
return
if _AUTH_MODE == 'open':
return # gate is open — no credential needed at all
try:
from lib.api_keys import bootstrap_personal_key, has_any_key
except Exception as _e:
logging.getLogger('server.boot').warning(
'[Auth] could not import bootstrap helpers: %s', _e)
return
if has_any_key():
return
plaintext = bootstrap_personal_key(name='personal')
if plaintext: