Skip to content

Instantly share code, notes, and snippets.

@calavera
Created June 30, 2026 00:04
Show Gist options
  • Select an option

  • Save calavera/315993443714d8cd749fe53c825da754 to your computer and use it in GitHub Desktop.

Select an option

Save calavera/315993443714d8cd749fe53c825da754 to your computer and use it in GitHub Desktop.
Tensorlake FE startup resolver repro using documentai/file-convert image
import http.server
import os
import socket
import threading
import time
from typing import Any
import httpx
from tensorlake.applications import Image, application, function
PYDANTIC_PIN = "pydantic==2.12.5"
file_convertion_image = (
Image(base_image="python:3.12-slim", name="documentai/file-convert")
.env("DEBIAN_FRONTEND", "noninteractive")
.run(
"apt-get update && apt-get install -y poppler-utils libreoffice libmagic1 openssl && rm -rf /var/lib/apt/lists/*"
)
.run("python -m pip install --upgrade pip wheel setuptools")
.run("pip install python-magic==0.4.27 markdownify")
.run("pip install requests boto3 modal google-genai==1.51.0")
.run(f"pip install '{PYDANTIC_PIN}'")
.run("pip install lxml==6.0.2 beautifulsoup4 cryptography==46.0.5 python-docx chardet")
.run("pip install pillow pandas openpyxl xlrd PyMuPDF pypdf pdf2image")
.run("pip cache purge")
)
class _HealthHandler(http.server.BaseHTTPRequestHandler):
def do_GET(self) -> None:
self.send_response(204)
self.end_headers()
def log_message(self, _format: str, *args: Any) -> None:
return
def _capture(name: str, fn: Any) -> dict:
start = time.monotonic()
try:
return {
"name": name,
"ok": True,
"duration_sec": round(time.monotonic() - start, 6),
"result": fn(),
}
except BaseException as e:
return {
"name": name,
"ok": False,
"duration_sec": round(time.monotonic() - start, 6),
"error_type": type(e).__name__,
"error": str(e),
}
def _addrinfo(name: str) -> list[dict]:
return [
{
"family": family,
"socktype": socktype,
"proto": proto,
"canonname": canonname,
"sockaddr": sockaddr,
}
for family, socktype, proto, canonname, sockaddr in socket.getaddrinfo(name, 0)
]
def _threading_http_server_on_localhost() -> dict:
httpd = http.server.ThreadingHTTPServer(("localhost", 0), _HealthHandler)
try:
host, port = httpd.server_address[:2]
thread = threading.Thread(target=httpd.serve_forever, daemon=True)
thread.start()
response = httpx.get(f"http://localhost:{port}", timeout=5.0)
return {
"server_address": [host, port],
"client_status_code": response.status_code,
}
finally:
httpd.shutdown()
httpd.server_close()
def _httpx_client_with_localhost_base_url() -> dict:
httpd = http.server.ThreadingHTTPServer(("localhost", 0), _HealthHandler)
try:
port = httpd.server_address[1]
base_url = f"http://localhost:{port}"
client = httpx.Client(base_url=base_url, timeout=5.0)
client.close()
return {"base_url": base_url}
finally:
httpd.server_close()
def _collect_fe_startup_repro_checks() -> dict:
hostname = socket.gethostname()
checks = [
_capture("getaddrinfo_localhost", lambda: _addrinfo("localhost")),
_capture("getaddrinfo_hostname", lambda: _addrinfo(hostname)),
_capture("getaddrinfo_tl_sbx", lambda: _addrinfo("tl-sbx")),
_capture("threading_http_server_localhost", _threading_http_server_on_localhost),
_capture("httpx_client_localhost_base_url", _httpx_client_with_localhost_base_url),
]
return {
"hostname": hostname,
"hosts_file_exists": os.path.exists("/etc/hosts"),
"nsswitch_file_exists": os.path.exists("/etc/nsswitch.conf"),
"checks": checks,
}
# This runs when FE initialize imports the application module.
MODULE_IMPORT_FE_STARTUP_REPRO = _collect_fe_startup_repro_checks()
@application()
@function(
description="Reproduce FE initialize/startup resolver behavior",
image=file_convertion_image,
)
def localhost_resolver_check(_: str = "") -> dict:
return {
"module_import_fe_startup_repro": MODULE_IMPORT_FE_STARTUP_REPRO,
"invocation_repro": _collect_fe_startup_repro_checks(),
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment