nim-lang / Nim

Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
https://nim-lang.org
Other
16.41k stars 1.47k forks source link

Opening a number of DB connections with db_postgres can crash the program #20231

Closed jfilby closed 2 years ago

jfilby commented 2 years ago

What happened?

I've attached a program that opens and closes 100 DB connections with db_postgres. Most of the time it fails for me, the program aborts.

If I use a lock around the open() call to open the DB connection it works every time. However such a lock would be better placed in the db_postgres module to save others from this problem.

The same issue could also apply to db_sqlite and db_mysql.

I wrote this minimal test case because I found that DB queries were sometimes being corrupted in a larger program.

data_race_db_conn_test.zip

Nim Version

1.6.6

Current Standard Output Logs

Traceback (most recent call last)
...\.choosenim\toolchains\nim-1.6.6\lib\pure\concurrency\threadpool.nim(368) slave
...\db_conn_data_race_test.nim(40) dbThreadWrapper
...\db_thread.nim(7) dbThread
...\.choosenim\toolchains\nim-1.6.6\lib\impure\db_postgres.nim(637) open
SIGABRT: Abnormal termination.

Expected Standard Output Logs

No SIGABRT.

Possible Solution

Use a lock in db_postgres' open() proc, and possibly for db_sqlite and db_mysql too.

Additional Information

No response

Araq commented 2 years ago

Nim wraps the C libraries and is not responsible for bugs and omissions in the C libs. Nor does it add thread safety.

jfilby commented 2 years ago

OK I've added a lock to the Nexus framework for this purpose then.