Skip to content

Commit 3d131c9

Browse files
author
Li Zhaoxing
committed
Avoid process crash when elasticsearch response is nil
Add nil pointer check in elasticsearch bulkprocessor's after callback, defined inside pkg/es/config/config.go. In some rare cases, elasticsearch request will return error with nil response, and then nil response will be passed into after callback, and using of nil pointer will cause process crash. Signed-off-by: Li Zhaoxing <lizhaoxing@4paradigm.com>
1 parent 64f8bce commit 3d131c9

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

pkg/es/config/config.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func (c *Configuration) NewClient(logger *zap.Logger, metricsFactory metrics.Fac
114114
m.Delete(id)
115115

116116
// log individual errors, note that err might be false and these errors still present
117-
if response.Errors {
117+
if response != nil && response.Errors {
118118
for _, it := range response.Items {
119119
for key, val := range it {
120120
if val.Error != nil {
@@ -127,13 +127,21 @@ func (c *Configuration) NewClient(logger *zap.Logger, metricsFactory metrics.Fac
127127

128128
sm.Emit(err, time.Since(start.(time.Time)))
129129
if err != nil {
130-
failed := len(response.Failed())
130+
var failed int
131+
var respval interface{}
132+
if response == nil {
133+
failed = 0
134+
respval = "nil"
135+
} else {
136+
failed = len(response.Failed())
137+
respval = response
138+
}
131139
total := len(requests)
132140
logger.Error("Elasticsearch could not process bulk request",
133141
zap.Int("request_count", total),
134142
zap.Int("failed_count", failed),
135143
zap.Error(err),
136-
zap.Any("response", response))
144+
zap.Any("response", respval))
137145
}
138146
}).
139147
BulkSize(c.BulkSize).

0 commit comments

Comments
 (0)