[
https://issues.apache.org/jira/browse/KNOX-3252?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Tamás Hanicz updated KNOX-3252:
-------------------------------
Description:
The issue is a race condition during table creation when multiple Knox
instances are starting up simultaneously. At the initial "isTableExists" check
the tables are not yet created. So Knox-instance-0 and Knox-instance-1 will
both go ahead with the creation. Postgres will create a record in pg_class and
pg_type as well. However the process that gets there later will get the
exception due to the record that was created by the other process in pg_type.
I was able to reproduce it locally. I started up a local Knox instance and I
had a Postgres db running on my docker desktop. I added a breakpoint to the
table creation and created a new type called "knox_tokens" manually.
{code:java}
CREATE TYPE knox_tokens AS ENUM ('active', 'expired', 'revoked');After this
proceeding with the Knox process I got the exception. Solution is catching this
exception validating the SQL state and skip table creation entirely in that
Knox instance.{code}
{code:java}
2026-02-10 21:19:44,825 ERROR knox.gateway
(TokenStateServiceFactory.java:createService(63)) - Error while initiatalizing
org.apache.knox.gateway.services.token.impl.JDBCTokenStateService: Error while
initiating JDBCTokenStateService: org.postgresql.util.PSQLException: ERROR:
type "knox_tokens" already exists{code}
was:
The issue is a race condition during table creation when multiple Knox
instances are starting up simultaneously. At the initial "isTableExists" check
the tables are not yet created. So Knox-instance-0 and Knox-instance-1 will
both go ahead with the creation. Postgres will create a record in pg_class and
pg_type as well. However the process that gets there later will get the
exception due to the record that was created by the other process in pg_type.
I was able to reproduce it locally. I started up a local Knox instance and I
had a Postgres db running on my docker desktop. I added a breakpoint to the
table creation and created a new type called "knox_tokens" manually.
{{}}
{code:java}
CREATE TYPE knox_tokens AS ENUM ('active', 'expired', 'revoked');{code}
{{}}
After this proceeding with the Knox process I got the exception. Solution is
catching this exception validating the SQL state and skip table creation
entirely in that Knox instance.
{code:java}
2026-02-10 21:19:44,825 ERROR knox.gateway
(TokenStateServiceFactory.java:createService(63)) - Error while initiatalizing
org.apache.knox.gateway.services.token.impl.JDBCTokenStateService: Error while
initiating JDBCTokenStateService: org.postgresql.util.PSQLException: ERROR:
type "knox_tokens" already exists{code}
> Intermittent Postgres type already exists exception
> ---------------------------------------------------
>
> Key: KNOX-3252
> URL: https://issues.apache.org/jira/browse/KNOX-3252
> Project: Apache Knox
> Issue Type: Bug
> Components: Server
> Affects Versions: 2.1.0
> Reporter: Tamás Hanicz
> Assignee: Tamás Hanicz
> Priority: Major
>
> The issue is a race condition during table creation when multiple Knox
> instances are starting up simultaneously. At the initial "isTableExists"
> check the tables are not yet created. So Knox-instance-0 and Knox-instance-1
> will both go ahead with the creation. Postgres will create a record in
> pg_class and pg_type as well. However the process that gets there later will
> get the exception due to the record that was created by the other process in
> pg_type.
> I was able to reproduce it locally. I started up a local Knox instance and I
> had a Postgres db running on my docker desktop. I added a breakpoint to the
> table creation and created a new type called "knox_tokens" manually.
> {code:java}
> CREATE TYPE knox_tokens AS ENUM ('active', 'expired', 'revoked');After this
> proceeding with the Knox process I got the exception. Solution is catching
> this exception validating the SQL state and skip table creation entirely in
> that Knox instance.{code}
> {code:java}
> 2026-02-10 21:19:44,825 ERROR knox.gateway
> (TokenStateServiceFactory.java:createService(63)) - Error while
> initiatalizing
> org.apache.knox.gateway.services.token.impl.JDBCTokenStateService: Error
> while initiating JDBCTokenStateService: org.postgresql.util.PSQLException:
> ERROR: type "knox_tokens" already exists{code}
--
This message was sent by Atlassian Jira
(v8.20.10#820010)