Edgewall Software
Modify

Opened 3 months ago

Closed 2 months ago

#13793 closed defect (fixed)

gitea git commit hooks failing to trac upgrade of trac.db.api.DatabaseManager

Reported by: lr@… Owned by: Jun Omae
Priority: normal Milestone: 1.6.1
Component: database backend Version: 1.6
Severity: normal Keywords: postgresql
Cc: Branch:
Release Notes:
API Changes:
Internal Changes:

Description (last modified by Jun Omae)

Since our upgrade to Trac 1.6, we've been having this issue with our git commit hooks giving error

Error: Unable to check for upgrade of trac.db.api.DatabaseManager: TimeoutError: Unable to get database connection within 0 seconds.

As documented here:

https://trac.osgeo.org/osgeo/ticket/3286

Our commit hooks are doing nothing but something like:

trac-admin /var/www/trac/env/postgis/ changeset added git bdecd88fe3f506b2b1f64de3f6cf25dc8f102f66

under our git user account.

We've manually tried these commands under our git account and they succeed whenever we do them, just not working under our git commit hooks.

We are at a loss to troubleshoot this, as there doesn't seem to be any log entry in the postgresql logs about this.

attached is a sample of one of our git hooks which is run under the post-receive git hook.

Attachments (1)

postreceive-git-hook.txt (1.5 KB ) - added by lr@… 3 months ago.
git hook

Download all attachments as: .zip

Change History (12)

by lr@…, 3 months ago

Attachment: postreceive-git-hook.txt added

git hook

comment:1 by Jun Omae, 3 months ago

Keywords: needinfo added

Please share trac.log in your Trac environment. The traceback of the error might be loggged.

Also, trac-admin command should be invoked with the same user of Trac web process, instead of git user (e.g. www-data user in the case of apache with mod-wsgi).

in reply to:  1 ; comment:2 by anonymous, 3 months ago

Replying to Jun Omae:

Please share trac.log in your Trac environment. The traceback of the error might be loggged.

Also, trac-admin command should be invoked with the same user of Trac web process, instead of git user (e.g. www-data user in the case of apache with mod-wsgi).

I have to review how our gitea app is set up cause that runs under the git user and is the one triggering the git web hook.

Here is what the trac log shows when it fails.

TypeError: sequence item 3: expected str instance, NoneType found
[2024-10-09 17:25:03,202] postgis:env WARNING: Component <Component trac.db.api.DatabaseManager> failed with TimeoutError: Unable to get database connection within 0 seconds.


comment:3 by anonymous, 3 months ago

Forgot to mention our backend database is postgresql 16 if that matters.

I tried the commands both under git account and www-data and they both work

e.g.

sudo -u git trac-admin /var/www/trac/env/postgis/ changeset added git 77eddb114e21ec0b7eba2b5caa496d7e864ef247

or

sudo -u www-data trac-admin /var/www/trac/env/postgis/ changeset added git 77eddb114e21ec0b7eba2b5caa496d7e864ef247

in reply to:  2 comment:4 by Jun Omae, 3 months ago

Component: generaldatabase backend
Keywords: postgresql added; needinfo removed

Replying to anonymous:

Replying to Jun Omae:

Please share trac.log in your Trac environment. The traceback of the error might be loggged.

Also, trac-admin command should be invoked with the same user of Trac web process, instead of git user (e.g. www-data user in the case of apache with mod-wsgi).

I have to review how our gitea app is set up cause that runs under the git user and is the one triggering the git web hook.

I'd suggest to be the same user to use for trac-admin via gitea and mod-wsgi processes, in order to prevent several troubles. Because Trac environment, especially trac.log, will be written from both processes. The user of mod-wsgi could be changed using WSGIDaemonProcess user parameter, if the user of gitea cannot be changed.

Here is what the trac log shows when it fails.

TypeError: sequence item 3: expected str instance, NoneType found
[2024-10-09 17:25:03,202] postgis:env WARNING: Component <Component trac.db.api.DatabaseManager> failed with TimeoutError: Unable to get database connection within 0 seconds.

Thanks. However, it is hard to investigate it cause of no traceback….

comment:5 by Jun Omae, 3 months ago

Description: modified (diff)

comment:6 by Jun Omae, 3 months ago

Owner: set to Jun Omae
Status: newassigned

I consider that PostgreSQLConnection or psycopg2 module raises the TypeError due to something wrong when trac-admin is invoked from post-commit hook. Also, Trac doesn't unfortunately log when the exception is raised.

Please try to enable [trac] debug_sql option to log the exception, and report the log if the same is encountered.

We should log the exception when failure to connect the database, regardless of the option.

  • trac/db/api.py

    diff --git a/trac/db/api.py b/trac/db/api.py
    index 9e80ee1a9..a95a19142 100644
    a b class DatabaseManager(Component):  
    417417        if not self._cnx_pool:
    418418            connector, args = self.get_connector()
    419419            self._cnx_pool = ConnectionPool(5, connector, **args)
    420         db = self._cnx_pool.get_cnx(self.timeout or None)
     420        db = self._cnx_pool.get_cnx(self.timeout or None, self.log)
    421421        if readonly:
    422422            db = ConnectionWrapper(db, readonly=True)
    423423        return db
  • trac/db/pool.py

    diff --git a/trac/db/pool.py b/trac/db/pool.py
    index b97610db2..b6cadd792 100644
    a b class ConnectionPoolBackend(object):  
    6565        self._pool_time = []
    6666        self._waiters = 0
    6767
    68     def get_cnx(self, connector, kwargs, timeout=None):
     68    def get_cnx(self, connector, kwargs, timeout=None, log=None):
    6969        cnx = None
    70         log = kwargs.get('log')
    7170        key = str(kwargs)
    7271        start = time_now()
    7372        tid = get_thread_id()
    class ConnectionPool(object):  
    210209        self._connector = connector
    211210        self._kwargs = kwargs
    212211
    213     def get_cnx(self, timeout=None):
    214         return _backend.get_cnx(self._connector, self._kwargs, timeout)
     212    def get_cnx(self, timeout=None, log=None):
     213        return _backend.get_cnx(self._connector, self._kwargs, timeout, log)
    215214
    216215    def shutdown(self, tid=None):
    217216        _backend.shutdown(tid)

comment:7 by anonymous, 3 months ago

Thanks for all the help that helped a lot.

Now we are seeing:

psycopg2.OperationalError: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: fe_sendauth: no password supplied

So I think that answers the question. Got to check why it's not getting the password in the config I guess when it does with direct tracadmin calls.

comment:8 by anonymous, 3 months ago

Okay I'm still a bit puzzled. If I set postgresql to trust mode for local connections it works as expected so that does seem to be the culprit.

I checked both the git account and the www-data account and both do have .pgpass with the user name trac and password used to connect to all our trac databases, and I confirmed logging in as git or www-data, I can connect to the databases via psql not providing a password. It also doesn't explain why calling tracadmin direct from either of those accounts works fine.

Here is the full trace:

[2024-10-10 08:20:42,523] postgis:env ERROR: Exception caught while checking for upgrade: TracError: Unable to check for upgrade of trac.db.api.DatabaseManager: TimeoutError: Unable to get database connection within 0 seconds.
[2024-10-10 08:20:43,151] postgis:pool ERROR: Exception caught on ping
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/dist-packages/trac/db/pool.py", line 99, in get_cnx
    cnx.ping()
  File "/usr/local/lib/python3.11/dist-packages/trac/db/postgres_backend.py", line 407, in ping
    cursor.execute('SELECT 1')
psycopg2.OperationalError: server closed the connection unexpectedly
        This probably means the server terminated abnormally
        before or while processing the request.

[2024-10-10 08:22:08,794] postgis:pool ERROR: Exception caught on ping
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/dist-packages/trac/db/pool.py", line 99, in get_cnx
    cnx.ping()
  File "/usr/local/lib/python3.11/dist-packages/trac/db/postgres_backend.py", line 407, in ping
    cursor.execute('SELECT 1')
psycopg2.OperationalError: server closed the connection unexpectedly
        This probably means the server terminated abnormally
        before or while processing the request.

[2024-10-10 08:26:19,219] postgis:pool ERROR: Exception caught on create
Traceback (most recent call last):
  File "/usr/lib/python3.11/cmd.py", line 214, in onecmd
    func = getattr(self, 'do_' + cmd)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'TracAdmin' object has no attribute 'do_changeset'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.11/dist-packages/trac/db/pool.py", line 103, in get_cnx
    cnx = connector.get_connection(**kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/trac/db/postgres_backend.py", line 150, in get_connection
    cnx = PostgreSQLConnection(path, log, user, password, host, port,
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/trac/db/postgres_backend.py", line 330, in __init__
    cnx = psycopg.connect(assemble_pg_dsn(path, user, password, host,
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/psycopg2/__init__.py", line 122, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
psycopg2.OperationalError: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: fe_sendauth: no password supplied

comment:9 by Jun Omae, 3 months ago

Thanks for the feedback. The root cause is on configurations of pg_hba.conf and [trac] database. I'd suggest asking on PostgreSQL users mailing list or forum about the authentication settings with socket.

I think that TypeError: sequence item 3: expected str instance, NoneType found is probably logged by another issue.

in reply to:  9 ; comment:10 by Jun Omae, 3 months ago

Replying to Jun Omae:

Thanks for the feedback. The root cause is on configurations of pg_hba.conf and [trac] database. I'd suggest asking on PostgreSQL users mailing list or forum about the authentication settings with socket.

In addition, check postgresql log for the authentication and authorization issues.

in reply to:  10 comment:11 by lr@…, 2 months ago

Resolution: fixed
Status: assignedclosed

Replying to Jun Omae:

Replying to Jun Omae:

Thanks for the feedback. The root cause is on configurations of pg_hba.conf and [trac] database. I'd suggest asking on PostgreSQL users mailing list or forum about the authentication settings with socket.

In addition, check postgresql log for the authentication and authorization issues.

Thanks for all the help. We finally figured out the issue, after checking what environment variables gitea was using.

The culprit was that gitea overrides the home variable to:

HOME=/home/git/gitea/data/home

So it was expecting the PostgreSQL .pgpass file to be there instead of /home/git.

Copying the .pgpass file there fixed the issue.

Modify Ticket

Change Properties
Set your email in Preferences
Action
as closed The owner will remain Jun Omae.
The resolution will be deleted. Next status will be 'reopened'.
to The owner will be changed from Jun Omae to the specified user.

Add Comment


E-mail address and name can be saved in the Preferences .
 
Note: See TracTickets for help on using tickets.