Skip to content

Commit 6136b78

Browse files
committed
Fix possible lock-ups when a trigger triggers a trigger.
Hanging was possible on the subsequent try to acquire a mutex. Now it is correctly implemented and if a trigger is being executed then no other trigger will be fired, on recursive calls.
1 parent 3d733ce commit 6136b78

File tree

1 file changed

+16
-37
lines changed

1 file changed

+16
-37
lines changed

ext/mysqlnd/mysqlnd_statistics.h

Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -42,24 +42,28 @@ extern const MYSQLND_STRING mysqlnd_stats_values_names[];
4242
#define MYSQLND_STATS_UNLOCK(stats)
4343
#endif
4444

45-
#define MYSQLND_CHECK_AND_CALL_HANDLER(stats, statistic, value) \
45+
#define MYSQLND_UPDATE_VALUE_AND_CALL_TRIGGER(stats, statistic, value) \
4646
{ \
47+
MYSQLND_STATS_LOCK(stats); \
48+
(stats)->values[(statistic)] += (value); \
4749
if ((stats)->triggers[(statistic)] && (stats)->in_trigger == FALSE) { \
4850
(stats)->in_trigger = TRUE; \
51+
MYSQLND_STATS_UNLOCK(stats); \
52+
\
4953
(stats)->triggers[(statistic)]((stats), (statistic), (value) TSRMLS_CC); \
54+
\
55+
MYSQLND_STATS_LOCK(stats); \
5056
(stats)->in_trigger = FALSE; \
5157
} \
58+
MYSQLND_STATS_UNLOCK(_p_s); \
5259
} \
5360

5461
#define MYSQLND_DEC_STATISTIC(enabler, stats, statistic) \
5562
{ \
5663
enum_mysqlnd_collected_stats _s = (statistic);\
5764
MYSQLND_STATS * _p_s = (MYSQLND_STATS *) (stats); \
5865
if ((enabler) && _p_s && _s != _p_s->count) { \
59-
MYSQLND_STATS_LOCK(_p_s); \
60-
MYSQLND_CHECK_AND_CALL_HANDLER(_p_s, _s, -1); \
61-
_p_s->values[_s]--; \
62-
MYSQLND_STATS_UNLOCK(_p_s); \
66+
MYSQLND_UPDATE_VALUE_AND_CALL_TRIGGER(_p_s, _s, -1); \
6367
}\
6468
}
6569

@@ -68,10 +72,7 @@ extern const MYSQLND_STRING mysqlnd_stats_values_names[];
6872
enum_mysqlnd_collected_stats _s = (statistic);\
6973
MYSQLND_STATS * _p_s = (MYSQLND_STATS *) (stats); \
7074
if ((enabler) && _p_s && _s != _p_s->count) { \
71-
MYSQLND_STATS_LOCK(_p_s); \
72-
MYSQLND_CHECK_AND_CALL_HANDLER(_p_s, _s, 1); \
73-
_p_s->values[_s]++; \
74-
MYSQLND_STATS_UNLOCK(_p_s); \
75+
MYSQLND_UPDATE_VALUE_AND_CALL_TRIGGER(_p_s, _s, 1); \
7576
}\
7677
}
7778

@@ -81,10 +82,7 @@ extern const MYSQLND_STRING mysqlnd_stats_values_names[];
8182
MYSQLND_STATS * _p_s = (MYSQLND_STATS *) (stats); \
8283
if ((enabler) && _p_s && _s != _p_s->count) { \
8384
uint64_t v = (uint64_t) (value); \
84-
MYSQLND_STATS_LOCK(_p_s); \
85-
MYSQLND_CHECK_AND_CALL_HANDLER(_p_s, _s, v); \
86-
_p_s->values[_s] += v; \
87-
MYSQLND_STATS_UNLOCK(_p_s); \
85+
MYSQLND_UPDATE_VALUE_AND_CALL_TRIGGER(_p_s, _s, v); \
8886
}\
8987
}
9088

@@ -96,16 +94,8 @@ extern const MYSQLND_STRING mysqlnd_stats_values_names[];
9694
uint64_t v2 = (uint64_t) (value2); \
9795
enum_mysqlnd_collected_stats _s1 = (statistic1);\
9896
enum_mysqlnd_collected_stats _s2 = (statistic2);\
99-
MYSQLND_STATS_LOCK(_p_s); \
100-
if (_s1 != _p_s->count) { \
101-
MYSQLND_CHECK_AND_CALL_HANDLER(_p_s, _s1, v1); \
102-
_p_s->values[_s1]+= v1; \
103-
} \
104-
if (_s2 != _p_s->count) { \
105-
MYSQLND_CHECK_AND_CALL_HANDLER(_p_s, _s2, v2); \
106-
_p_s->values[_s2]+= v2; \
107-
} \
108-
MYSQLND_STATS_UNLOCK(_p_s); \
97+
if (_s1 != _p_s->count) MYSQLND_UPDATE_VALUE_AND_CALL_TRIGGER(_p_s, _s1, v1); \
98+
if (_s2 != _p_s->count) MYSQLND_UPDATE_VALUE_AND_CALL_TRIGGER(_p_s, _s2, v2); \
10999
}\
110100
}
111101

@@ -119,20 +109,9 @@ extern const MYSQLND_STRING mysqlnd_stats_values_names[];
119109
enum_mysqlnd_collected_stats _s1 = (statistic1);\
120110
enum_mysqlnd_collected_stats _s2 = (statistic2);\
121111
enum_mysqlnd_collected_stats _s3 = (statistic3);\
122-
MYSQLND_STATS_LOCK(_p_s); \
123-
if (_s1 != _p_s->count) { \
124-
MYSQLND_CHECK_AND_CALL_HANDLER(_p_s, _s1, v1); \
125-
_p_s->values[_s1]+= v1; \
126-
} \
127-
if (_s2 != _p_s->count) { \
128-
MYSQLND_CHECK_AND_CALL_HANDLER(_p_s, _s2, v2); \
129-
_p_s->values[_s2]+= v2; \
130-
} \
131-
if (_s3 != _p_s->count) { \
132-
MYSQLND_CHECK_AND_CALL_HANDLER(_p_s, _s3, v3); \
133-
_p_s->values[_s3]+= v3; \
134-
} \
135-
MYSQLND_STATS_UNLOCK(_p_s); \
112+
if (_s1 != _p_s->count) MYSQLND_UPDATE_VALUE_AND_CALL_TRIGGER(_p_s, _s1, v1); \
113+
if (_s2 != _p_s->count) MYSQLND_UPDATE_VALUE_AND_CALL_TRIGGER(_p_s, _s2, v2); \
114+
if (_s3 != _p_s->count) MYSQLND_UPDATE_VALUE_AND_CALL_TRIGGER(_p_s, _s3, v3); \
136115
}\
137116
}
138117

0 commit comments

Comments
 (0)