Commit Graph

93 Commits

Author SHA1 Message Date
Aaron Kling
c4683d90a9 Format code using astyle google format
Commands used:
astyle --style=google --indent=spaces=2 --keep-one-line-blocks src/*.cpp
astyle --style=google --indent=spaces=2 --keep-one-line-blocks src/*.h
2024-03-26 13:43:58 -05:00
Isaac Connor
f5cdab131f Include rc in debug 2024-03-04 14:13:41 -05:00
Isaac Connor
067ca68549 Add debugging of db failures 2024-03-04 13:20:21 -05:00
Isaac Connor
b9da90522b Wait forever until reconnect, the fallthrough case had us retrying the sql on a closed connection which may have led to a crash. 2024-03-04 09:32:15 -05:00
Isaac Connor
72cbf4a393 Include unistd because we use sleep 2024-02-20 20:29:49 -05:00
Isaac Connor
e03afd678a Add a return value to zmDbReconnect which seems to fix the crashing 2024-02-20 19:31:11 -05:00
Isaac Connor
93ee735086 Fix nitpick 2024-02-20 18:56:54 -05:00
Isaac Connor
87156149a0 Only do mysql_close if we think we are connected 2024-02-20 18:56:02 -05:00
Isaac Connor
e9e429083e Add zmDbReconnect, which closes the connection before trying to reconnect, in an attempt to prevent mem leak. This adds a 1 sec sleep if we fail to connect. This also removes the disabling of db logging before doing logging... because we are going to assume (possibly incorrectly) that the logging code is correct and generating well-formed sql, and hence the error is external. This might be a bad idea. 2024-02-20 18:36:10 -05:00
Isaac Connor
aaa55b9776 Improve debug logging when failed to connect to db, and provide reason when fail to run query 2024-01-23 16:11:44 -05:00
Isaac Connor
e132181f6a Make reconnection logging a debug 2023-12-08 17:42:50 -05:00
Isaac Connor
70c5dc6fa9 Replace deprecated mysql_ssl_set with mysql_options() 2023-10-30 16:07:34 -04:00
Isaac Connor
3346a67b94 set zmDbConnected to false before calling zmDbConnect 2023-09-25 17:17:38 -04:00
Isaac Connor
df8f725afa Auto reconnect when mysql is lost 2023-09-22 13:35:30 -04:00
Isaac Connor
22d911940f Remove deprecated reconnect setting for mysql 2023-09-12 15:10:34 -04:00
Isaac Connor
e49876de53 Bump number of db queue entries before warning to 40. 40 is still a reasonable number and rarely hit in testing. 2022-10-13 17:36:41 -04:00
Isaac Connor
619cf1975f Add getting the connection id from mysql and log it in zmDbDo. This is so that when mysql reports a dropped connection, we can figure out which process it was. 2022-10-06 10:28:49 -04:00
Ratchanan Srirattanamet
d0a7b51283 db: start the processing thread after all fields are initialized
Initializing `zmDbQueue::mThread` with initializer list makes the thread
start in the middle of object initialization. This means `zmDbQueue::
process()` could start and reach mCondition.wait(lock); before `std::
wait_condition`'s constructor, which causes deadlock.

To make sure thread won't start before object initialize, move
`mThread`'s initialization to constructor's body.

This _at last_ solves the random zmu deadlock issue that has plagued my
setup for almost a year.
2022-08-08 03:13:08 +07:00
Isaac Connor
1531ced590 Remove unneeded debug 2022-06-08 16:44:47 -04:00
Isaac Connor
6aeb163cba Fix lock around terminate 2022-03-13 08:37:33 -04:00
Isaac Connor
fc21fb643e Release lock before notify 2022-02-20 10:33:13 -05:00
Isaac Connor
02ccf13c7b Merge branch 'master' of github.com:ZoneMinder/zoneminder 2022-02-20 10:32:43 -05:00
Isaac Connor
b896974a29 Should get lock before testing for connected 2022-02-20 10:32:40 -05:00
Ratchanan Srirattanamet
ebe502a747 db: fix dead lock in zmDbQueue::stop()
Notifying `mCondition` without taking the lock causes a race condition
in ::process() between checking `mTerminate` and waiting for the
`mCondition`, which causes a dead lock. This commit moves writing to
`mTerminate` and notifying `mCondition` under the lock to eliminate race
condition and dead lock.

This is not theoretical. It has caused zmu to hang at exit on a
Raspberry Pi 4, exhuasting PHP-FPM process pool. The stacks below are
captured when running ZoneMinder 1.36.12-focal1 on Ubuntu 20.04:

(gdb) thread apply all bt

Thread 2 (Thread 0xffff80c1c880 (LWP 259988)):
#0  futex_wait_cancelable (private=0, expected=0, futex_word=0xaaaae0584e80 <dbQueue+176>) at ../sysdeps/nptl/futex-internal.h:183
#1  __pthread_cond_wait_common (abstime=0x0, clockid=0, mutex=0xaaaae0584e28 <dbQueue+88>, cond=0xaaaae0584e58 <dbQueue+136>) at pthread_cond_wait.c:508
#2  __pthread_cond_wait (cond=0xaaaae0584e58 <dbQueue+136>, mutex=0xaaaae0584e28 <dbQueue+88>) at pthread_cond_wait.c:638
#3  0x0000ffff8700d670 in std::condition_variable::wait(std::unique_lock<std::mutex>&) () from /lib/aarch64-linux-gnu/libstdc++.so.6
#4  0x0000aaaae0438f08 in zmDbQueue::process (this=0xaaaae0584dd0 <dbQueue>) at ./src/zm_db.cpp:250
#5  0x0000ffff87013fac in ?? () from /lib/aarch64-linux-gnu/libstdc++.so.6
#6  0x0000ffff891264fc in start_thread (arg=0xffffe60d84bf) at pthread_create.c:477
#7  0x0000ffff86dd767c in thread_start () at ../sysdeps/unix/sysv/linux/aarch64/clone.S:78

Thread 1 (Thread 0xffff80c23010 (LWP 259987)):
#0  __pthread_clockjoin_ex (threadid=281472841926784, thread_return=0x0, clockid=0, abstime=<optimized out>, block=<optimized out>) at pthread_join_common.c:145
#1  0x0000ffff87014240 in std::thread::join() () from /lib/aarch64-linux-gnu/libstdc++.so.6
#2  0x0000aaaae04314d0 in exit_zmu (exit_code=0) at ./src/zmu.cpp:200
#3  0x0000aaaae042f4c8 in main (argc=<optimized out>, argv=<optimized out>) at ./src/zmu.cpp:797
2022-02-20 09:58:03 +00:00
Isaac Connor
146ea4822d Bump the db queue limit to 30 before we warn. I only have 1 server that gets over 20 and it is still ok. 2022-01-24 09:24:16 -05:00
Isaac Connor
77d3109152 Increase to 20 before warning about db queue size. Put lock in it's own scope so that we unlock before notifying 2021-11-24 13:44:45 -05:00
Isaac Connor
61f7989bec Actually report the # of dbQueue entries 2021-11-02 17:24:05 -04:00
Isaac Connor
c2bd2dc129 Add a warning if db queue is larger than 10 2021-10-28 10:46:50 -04:00
ColorfullyZhang
6009bba339 Set mysql character set to utf8 explicitly to support chinese characters (or other special characters). 2021-09-16 14:41:24 +08:00
Peter Keresztes Schmidt
65656de6ce db: Adjust the query methods to accept std::strings 2021-07-06 10:20:46 +02:00
Peter Keresztes Schmidt
cf9c47149f db: Add helper for escaping strings and use it 2021-07-06 10:20:45 +02:00
Isaac Connor
b8e3cc33f0 make it so that the queue will more likely be empty on termination. Do not queue more sql if terminate flag is set. 2021-05-10 12:54:35 -04:00
Isaac Connor
76080cb857 add a stop function to dbQueue to clear out the queue before we kill log. 2021-04-23 09:25:07 -04:00
Isaac Connor
cbec5b2800 Implement zmDbDoUpdate which returns -mysql_errer or # of rows modified 2021-03-11 13:48:16 -05:00
Isaac Connor
43e7e612c5 Have to turn off DB logging when logging from a db query or else we infinite loop 2021-03-11 13:07:47 -05:00
Isaac Connor
1adeda6241 Add debugging of sql 2021-03-04 13:26:10 -05:00
Peter Keresztes Schmidt
3dd52a92eb db: Make sure to bind only rvalues when pushing to zmDbQueue
With this we can ensure that no copy-construction of the SQL string takes place.
Regarding calling semantics: The pushed SQL string will be moved and cannot be reused.
2021-02-25 23:08:43 +01:00
Isaac Connor
8aeb4ab758 Switch db_mutex to a std::mutex. Use modern locking with it. Use zmDbDo or dbQueue.push where appropriate. code cleanup. 2021-02-25 12:26:26 -05:00
Isaac Connor
fdf515ca10 rough in a db queue thread. Use it in zm_logger so that we don't have to aquire the db lock 2021-02-24 19:59:55 -05:00
Isaac Connor
a8e63e4f20 watch for zm_terminate in db while loops 2021-02-19 12:07:12 -05:00
Isaac Connor
63a45888a0 Introduce utility functions zmDbDo and zmDbDoInsert. 2021-02-18 16:01:30 -05:00
Peter Keresztes Schmidt
cbb37337cb DB: Improve resource cleanup on connection failure 2021-02-07 20:16:16 +01:00
Peter Keresztes Schmidt
cc6ea04afe Reformat touched code 2021-02-07 13:55:08 +01:00
Peter Keresztes Schmidt
3690da90ec DB: Call mysql_library_end when closing the connection
With the previous commit double initialization of the mysql library is avoided which could have led to the segfaults.
2021-02-07 13:46:33 +01:00
Peter Keresztes Schmidt
0dbc39ee25 Cleanup and reorganize includes
With this commit a unified structure for includes is introduced.
The general rules:
 * Only include what you need
 * Include wherever possible in the cpp and forward-declare in the header

 The includes are sorted in a local to global fashion. This means for the include order:
  0. If cpp file: The corresponding h file and an empty line
  1. Includes from the project sorted alphabetically
  2. System/library includes sorted alphabetically
  3. Conditional includes
2021-02-04 18:02:01 +01:00
Peter Keresztes Schmidt
5a57efdfe2 Replace deprecated C header includes with the C++ ones. 2021-02-04 05:39:03 +01:00
Isaac Connor
e496679efd Set transaction isolation level to READ COMMITTED so that concurrent event inserts don't deadlock 2021-01-12 14:19:04 -05:00
Isaac Connor
74972be9b5 spacing, code style. Set row=nullptr to quiet valgrind 2020-11-18 13:15:52 -05:00
Peter Keresztes Schmidt
8f980a1168 Convert NULL/0 to nullptr
The 0 -> nullptr changes should definitely improve readability.
2020-08-26 22:03:40 +02:00
Isaac Connor
efbab4e2bc Dont' call mysql_library_end as it segfaults and hangs. 2020-05-14 12:00:36 -04:00