pgwire icon indicating copy to clipboard operation
pgwire copied to clipboard

`secure_server` example causes psql and pgcli to segfault

Open samuelcolvin opened this issue 1 year ago • 6 comments

I'm on main, currently 30707e1, I can run the secure_server example with

cargo r --example secure_server

Then try to connect with psql, and I get the following:

➤ psql -p 5433 -U postgres -h 127.0.0.1                
psql (14.9 (Homebrew), server 0.19.2)
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
Type "help" for help.

zsh: segmentation fault  psql -p 5433 -U postgres -h 127.0.0.1

Similarly if I try to connect with pgcli, I get:

➤ pgcli 'postgresql://postgres:[email protected]:5433'
Server: PostgreSQL 0.19.2
Version: 4.0.1
Home: http://pgcli.com
postgres@127:(none)> Exception in thread completion_refresh:
Traceback (most recent call last):
  File "/Users/samuel/.pyenv/versions/3.10.13/lib/python3.10/threading.py", line 1016, in _bootstrap_inner
    self.run()
  File "/Users/samuel/.pyenv/versions/3.10.13/lib/python3.10/threading.py", line 953, in runreshing completions...                                                                                  
    self._target(*self._args, **self._kwargs)
  File "/Users/samuel/.pyenv/versions/3.10.13/lib/python3.10/site-packages/pgcli/completion_refresher.py", line 67, in _bg_refresh
    refresher(completer, executor)
  File "/Users/samuel/.pyenv/versions/3.10.13/lib/python3.10/site-packages/pgcli/completion_refresher.py", line 108, in refresh_schemata
    completer.set_search_path(executor.search_path())
  File "/Users/samuel/.pyenv/versions/3.10.13/lib/python3.10/site-packages/pgcli/pgcompleter.py", line 328, in set_search_path
    self.search_path = self.escaped_names(search_path)
  File "/Users/samuel/.pyenv/versions/3.10.13/lib/python3.10/site-packages/pgcli/pgcompleter.py", line 176, in escaped_names
    return [self.escape_name(name) for name in names]
TypeError: 'int' object is not iterable
Exception ignored in: <function BaseConnection.__del__ at 0x1063d4e50>
Traceback (most recent call last):
  File "/Users/samuel/.pyenv/versions/3.10.13/lib/python3.10/site-packages/psycopg/connection.py", line 158, in __del__
    f"connection {self} was deleted while still open."
  File "/Users/samuel/.pyenv/versions/3.10.13/lib/python3.10/site-packages/psycopg/connection.py", line 165, in __repr__
    info = connection_summary(self.pgconn)
  File "/Users/samuel/.pyenv/versions/3.10.13/lib/python3.10/site-packages/psycopg/pq/misc.py", line 136, in connection_summary
    if pgconn.user != pgconn.db:
  File "/Users/samuel/.pyenv/versions/3.10.13/lib/python3.10/site-packages/psycopg/pq/pq_ctypes.py", line 177, in user
    return self._call_bytes(impl.PQuser)
  File "/Users/samuel/.pyenv/versions/3.10.13/lib/python3.10/site-packages/psycopg/pq/pq_ctypes.py", line 711, in _call_bytes
    assert rv is not None
AssertionError: 
python(40911,0x16c24f000) malloc: double free for ptr 0x109810400
python(40911,0x16c24f000) malloc: *** set a breakpoint in malloc_error_break to debug
zsh: abort      pgcli 'postgresql://postgres:[email protected]:5433'

I'm not quite sure what's causing this behavior, or what to try next, but I thought it would be worthwhile reporting the issue.

samuelcolvin avatar Jan 31 '24 09:01 samuelcolvin

hi @samuelcolvin , thank you for reporting. I just reproduced this issue from my local environment. It's because pgcli sends sql query SELECT * FROM current_schemas(true) on startup to get schema list for its completion feature. However, as an example of this wire protocol library, this server doesn't cover sql parsing and schema resolution. It always returns fixed resultset

+----+--------+
| id | name   |
|----+--------|
| 0  | Tom    |
| 1  | Jerry  |
| 2  | <null> |
+----+--------+

So it's unexpected for pgcli and the error is thrown. But it doesn't block you from further usage of pgcli and pgwire example.

sunng87 avatar Jan 31 '24 12:01 sunng87

Thanks, what about the segmentation fault from psql? I believe psql doesn't issue any queries on connection.

samuelcolvin avatar Feb 05 '24 12:02 samuelcolvin

To be clear, I don't think the error is just from the unexpected query response - I've seen that too and it causes python exceptions. But not errors in the C code, also it doesn't explain the error from psql.

samuelcolvin avatar Feb 05 '24 12:02 samuelcolvin

That's weird I cannot reproduce it with psql 16.1 on archlinux.

psql -h 127.0.0.1 -p 5433   
psql (16.1, server 0.19.2)
WARNING: psql major version 16, server major version 0.19.
         Some psql features might not work.
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, compression: off)
Type "help" for help.

sunng=>

sunng87 avatar Feb 05 '24 13:02 sunng87

And off-topic, I'm curious that are you investigating pgwire for some new idea/project?

sunng87 avatar Feb 05 '24 13:02 sunng87

For the psql issue, I found it's an issue that can only reproduce on psql 14.x client compiled for homebrew.

psql: error: connection to server at “5s0kjjpih6a0.us-west-2.aws.greptime.cloud” (52.25.118.248), port 4003 failed: FATAL:  Database not specified
connection to server at “5s0kjjpih6a0.us-west-2.aws.greptime.cloud” (52.25.118.248), port 4003 failed: FATAL:  Database not specified
psql(33257,0x1de8e1000) malloc: double free for ptr 0x153808200
psql(33257,0x1de8e1000) malloc: *** set a breakpoint in malloc_error_break to debug

sunng87 avatar Mar 28 '24 20:03 sunng87