From 0d1fbef75109d807b64d2fae8b5939a95c276fab Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Wed, 30 Aug 2023 07:27:45 -0700 Subject: [PATCH] fix: a possible crash in event target Close() (#17948) these are possible crashes when the configured target is still in init() state and never finished - however a delete config was initiated. --- internal/event/target/mqtt.go | 4 +++- internal/event/target/mysql.go | 6 +++++- internal/event/target/postgresql.go | 6 +++++- internal/event/target/redis.go | 5 ++++- 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/internal/event/target/mqtt.go b/internal/event/target/mqtt.go index 171486e25..aa2a2475e 100644 --- a/internal/event/target/mqtt.go +++ b/internal/event/target/mqtt.go @@ -219,7 +219,9 @@ func (target *MQTTTarget) Save(eventData event.Event) error { // Close - does nothing and available for interface compatibility. func (target *MQTTTarget) Close() error { - target.client.Disconnect(100) + if target.client != nil { + target.client.Disconnect(100) + } close(target.quitCh) return nil } diff --git a/internal/event/target/mysql.go b/internal/event/target/mysql.go index bc6c53c84..48966f4b9 100644 --- a/internal/event/target/mysql.go +++ b/internal/event/target/mysql.go @@ -312,7 +312,11 @@ func (target *MySQLTarget) Close() error { _ = target.insertStmt.Close() } - return target.db.Close() + if target.db != nil { + return target.db.Close() + } + + return nil } // Executes the table creation statements. diff --git a/internal/event/target/postgresql.go b/internal/event/target/postgresql.go index 132b6d40c..736bafe33 100644 --- a/internal/event/target/postgresql.go +++ b/internal/event/target/postgresql.go @@ -308,7 +308,11 @@ func (target *PostgreSQLTarget) Close() error { _ = target.insertStmt.Close() } - return target.db.Close() + if target.db != nil { + target.db.Close() + } + + return nil } // Executes the table creation statements. diff --git a/internal/event/target/redis.go b/internal/event/target/redis.go index ad8d2a917..cc483404c 100644 --- a/internal/event/target/redis.go +++ b/internal/event/target/redis.go @@ -273,7 +273,10 @@ func (target *RedisTarget) SendFromStore(eventKey string) error { // Close - releases the resources used by the pool. func (target *RedisTarget) Close() error { close(target.quitCh) - return target.pool.Close() + if target.pool != nil { + return target.pool.Close() + } + return nil } func (target *RedisTarget) init() error {