@@ -60,16 +60,18 @@ async def server_lifespan(server: FastMCP) -> AsyncIterator[Dict[str, Any]]:
6060 server_version = ver_path .read_text (encoding = "utf-8" ).strip ()
6161 except Exception :
6262 server_version = "unknown"
63- # Defer telemetry for first second to avoid interfering with stdio handshake
64- if (time .perf_counter () - start_clk ) > 1.0 :
65- record_telemetry (RecordType .STARTUP , {
66- "server_version" : server_version ,
67- "startup_time" : start_time
68- })
69-
70- # Record first startup milestone
71- if (time .perf_counter () - start_clk ) > 1.0 :
72- record_milestone (MilestoneType .FIRST_STARTUP )
63+ # Defer initial telemetry by 1s to avoid stdio handshake interference
64+ import threading
65+ def _emit_startup ():
66+ try :
67+ record_telemetry (RecordType .STARTUP , {
68+ "server_version" : server_version ,
69+ "startup_time" : start_time ,
70+ })
71+ record_milestone (MilestoneType .FIRST_STARTUP )
72+ except Exception :
73+ logger .debug ("Deferred startup telemetry failed" , exc_info = True )
74+ threading .Timer (1.0 , _emit_startup ).start ()
7375
7476 try :
7577 skip_connect = os .environ .get ("UNITY_MCP_SKIP_STARTUP_CONNECT" , "" ).lower () in ("1" , "true" , "yes" , "on" )
@@ -79,33 +81,42 @@ async def server_lifespan(server: FastMCP) -> AsyncIterator[Dict[str, Any]]:
7981 _unity_connection = get_unity_connection ()
8082 logger .info ("Connected to Unity on startup" )
8183
82- # Record successful Unity connection
83- if (time .perf_counter () - start_clk ) > 1.0 :
84- record_telemetry (RecordType .UNITY_CONNECTION , {
84+ # Record successful Unity connection (deferred)
85+ import threading as _t
86+ _t .Timer (1.0 , lambda : record_telemetry (
87+ RecordType .UNITY_CONNECTION ,
88+ {
8589 "status" : "connected" ,
86- "connection_time_ms" : (time .time () - start_time ) * 1000
87- })
90+ "connection_time_ms" : (time .perf_counter () - start_clk ) * 1000 ,
91+ }
92+ )).start ()
8893
8994 except ConnectionError as e :
9095 logger .warning ("Could not connect to Unity on startup: %s" , e )
9196 _unity_connection = None
9297
93- # Record connection failure
94- if (time .perf_counter () - start_clk ) > 1.0 :
95- record_telemetry (RecordType .UNITY_CONNECTION , {
98+ # Record connection failure (deferred)
99+ import threading as _t
100+ _t .Timer (1.0 , lambda : record_telemetry (
101+ RecordType .UNITY_CONNECTION ,
102+ {
96103 "status" : "failed" ,
97104 "error" : str (e )[:200 ],
98- "connection_time_ms" : (time .perf_counter () - start_clk ) * 1000
99- })
105+ "connection_time_ms" : (time .perf_counter () - start_clk ) * 1000 ,
106+ }
107+ )).start ()
100108 except Exception as e :
101109 logger .warning ("Unexpected error connecting to Unity on startup: %s" , e )
102110 _unity_connection = None
103- if (time .perf_counter () - start_clk ) > 1.0 :
104- record_telemetry (RecordType .UNITY_CONNECTION , {
111+ import threading as _t
112+ _t .Timer (1.0 , lambda : record_telemetry (
113+ RecordType .UNITY_CONNECTION ,
114+ {
105115 "status" : "failed" ,
106116 "error" : str (e )[:200 ],
107- "connection_time_ms" : (time .perf_counter () - start_clk ) * 1000
108- })
117+ "connection_time_ms" : (time .perf_counter () - start_clk ) * 1000 ,
118+ }
119+ )).start ()
109120
110121 try :
111122 # Yield the connection object so it can be attached to the context
0 commit comments