perfetto icon indicating copy to clipboard operation
perfetto copied to clipboard

TraceProcessor Can not connect on Linux

Open lbknxy opened this issue 5 months ago • 6 comments

Unable to connect to the already started trace_processor_shell on Linux (Ubuntu 22.04). The startup and test code is as follows:

start service:

XXX@xxx:~/tmp_perfetto$ ./trace_processor_shell server http --port 9123 a.trace
[503.505]     common_flags.cc:464 Trace loaded: 316.33 MB in 6.57s (48.1 MB/s)
[503.505]       http_server.cc:67 [HTTP] Starting HTTP server on 127.0.0.1:9123
[503.505]       http_server.cc:78 [HTTP] Starting HTTP server on [::1]:9123
[503.505]          httpd.cc:116 [HTTP] This server can be used by reloading https://ui.perfetto.dev and clicking on YES on the "Trace Processor native acceleration" dialog or through the Python API (see https://perfetto.dev/docs/analysis/trace-processor#python-api).

connect

xxx@xxx:~/tmp_perfetto$ python test.py
A
B

Traceback (most recent call last):
  File "/home/sid/tmp_perfetto/test.py", line 6, in <module>
    for row in tp.query('SELECT ts, dur, name FROM slice limit 10'):
  File "/home/sid/.local/lib/python3.10/site-packages/perfetto/trace_processor/api.py", line 196, in query
    response = self.http.execute_query(sql)
  File "/home/sid/.local/lib/python3.10/site-packages/perfetto/trace_processor/http.py", line 32, in execute_query
    self.conn.request('POST', '/query', body=byte_data)
  File "/usr/lib/python3.10/http/client.py", line 1303, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/lib/python3.10/http/client.py", line 1349, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/usr/lib/python3.10/http/client.py", line 1298, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/usr/lib/python3.10/http/client.py", line 1058, in _send_output
    self.send(msg)
  File "/usr/lib/python3.10/http/client.py", line 996, in send
    self.connect()
  File "/usr/lib/python3.10/http/client.py", line 962, in connect
    self.sock = self._create_connection(
  File "/usr/lib/python3.10/socket.py", line 845, in create_connection
    raise err
  File "/usr/lib/python3.10/socket.py", line 833, in create_connection
    sock.connect(sa)
TimeoutError: [Errno 110] Connection timed out

code test.py almost copy from perfetto/ai/skills/perfetto/infra-references/querying.md:
xxx@xxx:~/tmp_perfetto$ cat test.py

from perfetto.trace_processor import TraceProcessor

print("A")
tp = TraceProcessor(addr='127.0.0.1:9123')
print("B")
for row in tp.query('SELECT ts, dur, name FROM slice limit 10'):
    print(row.dur, row.name)
print("C")
# Pandas is also supported if the dependency is installed:
df = tp.query('SELECT ts, dur, name FROM slice LIMIT 100').as_pandas_dataframe()
print("D")
tp.close()

I also tried it on Windows, and it works on Windows, but cannot connect under Linux. trace_processor_version: https://github.com/google/perfetto/releases/download/v56.0/linux-amd64.zip

lbknxy avatar Jun 09 '26 14:06 lbknxy

If it's working on Linux but not on Windows, it's likely some problem with your environment: please check on Linux if you have some firewall or daemon blocking access between Python <-> trace_processor. This is very well tested code internally (especially on Linux) so we would definitely have a lot of people complaining if something was broken here.

LalitMaganti avatar Jun 09 '26 14:06 LalitMaganti

Are you sure you don't have some special iptables / corporate firewall on? I can't think of many reasons why you wouldn't be able to connect to localhost.

what does sudo netstat -anp | grep 9123 show?

primiano avatar Jun 09 '26 14:06 primiano

I don't think there are any special firewall settings. Today I ran additional tests and reinstalled the Python Perfetto module, but the issue still persists.

xx@xxpc:~$ sudo ufw allow 9123
规则已添加
规则已添加 (v6)
xx@xxpc:~$ sudo ufw status
状态: 激活

至                          动作          来自
-                          --          --
10000                      ALLOW       Anywhere
...
9992                       ALLOW       Anywhere
9123                       ALLOW       Anywhere
....
9992 (v6)                  ALLOW       Anywhere (v6)
9123 (v6)                  ALLOW       Anywhere (v6)


sid@sidpc:~$ sudo netstat -anp | grep 9123
tcp        0      0 127.0.0.1:9123          0.0.0.0:*               LISTEN      2392863/./trace_pro
tcp6       0      0 ::1:9123                :::*                    LISTEN      2392863/./trace_pro
unix  3      [ ]         流        已连接     98912367 3675967/chrome --ty
...

then connect , based on the output of A and B from test.py, the connection to trace_processor_shell should be successful, but it timed out during the query.:

xx@xxpc:~/mcp_t1$ pip show perfetto

Name: perfetto
Version: 0.56.0
Summary: Python APIs and bindings for Perfetto (perfetto.dev)
Home-page: https://perfetto.dev/
Author: Perfetto
Author-email: [email protected]
License: apache-2.0
Location: /home/xx/.local/lib/python3.10/site-packages
Requires: protobuf
Required-by:
xx@xxpc:~/mcp_t1$
xx@xxpc:~/mcp_t1$ python test.py
A
B
Traceback (most recent call last):
  File "/home/xx/mcp_t1/test.py", line 8, in <module>
    for row in tp.query('SELECT ts, dur, name FROM slice limit 10'):
  File "/home/xx/.local/lib/python3.10/site-packages/perfetto/trace_processor/api.py", line 196, in query
    response = self.http.execute_query(sql)
  File "/home/xx/.local/lib/python3.10/site-packages/perfetto/trace_processor/http.py", line 32, in execute_query
    self.conn.request('POST', '/query', body=byte_data)
  File "/usr/lib/python3.10/http/client.py", line 1283, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/lib/python3.10/http/client.py", line 1329, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/usr/lib/python3.10/http/client.py", line 1278, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/usr/lib/python3.10/http/client.py", line 1038, in _send_output
    self.send(msg)
  File "/usr/lib/python3.10/http/client.py", line 976, in send
    self.connect()
  File "/usr/lib/python3.10/http/client.py", line 942, in connect
    self.sock = self._create_connection(
  File "/usr/lib/python3.10/socket.py", line 857, in create_connection
    raise err
  File "/usr/lib/python3.10/socket.py", line 845, in create_connection
    sock.connect(sa)
TimeoutError: [Errno 110] Connection timed out

lbknxy avatar Jun 10 '26 01:06 lbknxy

I found something:

On Linux must use:

tp = TraceProcessor(addr='http://127.0.0.1:9123')

On Windows, only the IP address also works:

tp = TraceProcessor(addr='127.0.0.1:9123')

lbknxy avatar Jun 10 '26 02:06 lbknxy

I think it's related to python version. can you confirm the version of python on your windows machine ? We use urlparse and I see some issues related with that https://github.com/python/cpython/issues/99418

sashwinbalaji avatar Jun 10 '26 09:06 sashwinbalaji

Agreed there is some weirdness going on with urlparse on some environments. I did the following test:

      for v in 3.8 3.9 3.10 3.11 3.12 3.13; do
        $UV run --quiet --python $v --no-project python -c "
      import sys
      from urllib.parse import urlparse
      for a in ['127.0.0.1:9123', 'localhost:9123']:
          p = urlparse(a)
          host = p.netloc if p.netloc else p.path
          print(f'{sys.version_info.major}.{sys.version_info.minor}: {a!r:24} scheme={p.scheme!r:12} old-code-host={host!r}')
      " 2>/dev/null || echo "$v: unavailable"
      done)
  ⎿  3.8: '127.0.0.1:9123'         scheme=''           old-code-host='127.0.0.1:9123'
     3.8: 'localhost:9123'         scheme=''           old-code-host='localhost:9123'
     3.9: '127.0.0.1:9123'         scheme='127.0.0.1'  old-code-host='9123'
     3.9: 'localhost:9123'         scheme='localhost'  old-code-host='9123'
     3.10: '127.0.0.1:9123'         scheme='127.0.0.1'  old-code-host='9123'
     3.10: 'localhost:9123'         scheme='localhost'  old-code-host='9123'
     3.11: '127.0.0.1:9123'         scheme=''           old-code-host='127.0.0.1:9123'
     3.11: 'localhost:9123'         scheme='localhost'  old-code-host='9123'
     3.12: '127.0.0.1:9123'         scheme=''           old-code-host='127.0.0.1:9123'
     3.12: 'localhost:9123'         scheme='localhost'  old-code-host='9123'
     3.13: '127.0.0.1:9123'         scheme=''           old-code-host='127.0.0.1:9123'
     3.13: 'localhost:9123'         scheme='localhost'  old-code-host='9123'

You can see on 3.8 and also 3.11+, things were working correctly with 127.0.0.1 but on 3.9 and 3.10, not so much. localhost continues to be broken regardless.

The simplest fix for this is to just prepend the scheme ourselves: https://github.com/google/perfetto/pull/6180 should fix this.

LalitMaganti avatar Jun 10 '26 09:06 LalitMaganti