Skip to content

Commit 6aa2773

Browse files
Reflect the review results.
1 parent 4d12928 commit 6aa2773

File tree

2 files changed

+42
-24
lines changed

2 files changed

+42
-24
lines changed

src/flb_lib.c

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -143,24 +143,25 @@ void flb_init_env()
143143
int flb_create_event_loop(flb_ctx_t *ctx)
144144
{
145145
int ret;
146-
struct flb_config *config = ctx->config;
146+
struct flb_config *config;
147+
148+
if (ctx == NULL)
149+
return FLB_LIB_ERROR;
150+
151+
config = ctx->config;
147152

148153
/* Create the event loop to receive notifications */
149154
ctx->event_loop = mk_event_loop_create(256);
150-
if (!ctx->event_loop) {
151-
flb_config_exit(ctx->config);
152-
flb_free(ctx);
155+
if (!ctx->event_loop)
153156
return FLB_LIB_ERROR;
154-
}
157+
155158
config->ch_evl = ctx->event_loop;
156159

157160
/* Prepare the notification channels */
158161
ctx->event_channel = flb_calloc(1, sizeof(struct mk_event));
159162
if (!ctx->event_channel) {
160163
perror("calloc");
161-
flb_config_exit(ctx->config);
162-
flb_free(ctx);
163-
return FLB_LIB_ERROR;
164+
goto error_1;
164165
}
165166

166167
MK_EVENT_ZERO(ctx->event_channel);
@@ -171,36 +172,48 @@ int flb_create_event_loop(flb_ctx_t *ctx)
171172
ctx->event_channel);
172173
if (ret != 0) {
173174
flb_error("[lib] could not create notification channels");
174-
flb_stop(ctx);
175-
flb_destroy(ctx);
176-
return FLB_LIB_ERROR;
175+
goto error_2;
177176
}
178177

179178
return 0;
179+
180+
error_2:
181+
flb_free(ctx->event_channel);
182+
ctx->event_channel = NULL;
183+
error_1:
184+
mk_event_loop_destroy(ctx->event_loop);
185+
ctx->event_loop = NULL;
186+
return FLB_LIB_ERROR;
180187
}
181188

182189
int flb_destroy_event_loop(flb_ctx_t *ctx)
183190
{
184191
int ret;
185192
struct flb_config *config;
186193

187-
if (ctx == NULL)
194+
if (ctx == NULL || ctx->config == NULL)
188195
return 0;
189196

190197
config = ctx->config;
191-
ret = mk_event_channel_destroy(config->ch_evl,
192-
config->ch_notif[0],
193-
config->ch_notif[1],
194-
ctx->event_channel);
195-
if (ret != 0) {
196-
/* make sure to close file descriptors */
197-
close(config->ch_notif[0]);
198-
close(config->ch_notif[1]);
198+
if (ctx->event_channel != NULL) {
199+
ret = mk_event_channel_destroy(config->ch_evl,
200+
config->ch_notif[0],
201+
config->ch_notif[1],
202+
ctx->event_channel);
203+
if (ret != 0) {
204+
/* make sure to close file descriptors */
205+
close(config->ch_notif[0]);
206+
close(config->ch_notif[1]);
207+
}
208+
free(ctx->event_channel);
209+
ctx->event_channel = NULL;
199210
}
200211

201-
free(ctx->event_channel);
212+
if (ctx->event_loop != NULL) {
213+
mk_event_loop_destroy(ctx->event_loop);
214+
ctx->event_loop = NULL;
215+
}
202216

203-
mk_event_loop_destroy(ctx->event_loop);
204217
return 0;
205218
}
206219

@@ -249,8 +262,11 @@ flb_ctx_t *flb_create()
249262
}
250263

251264
ret = flb_create_event_loop(ctx);
252-
if (ret != 0)
265+
if (ret != 0) {
266+
flb_config_exit(ctx->config);
267+
flb_free(ctx);
253268
return NULL;
269+
}
254270

255271
#ifdef FLB_HAVE_AWS_ERROR_REPORTER
256272
if (is_error_reporting_enabled()) {

src/fluent-bit.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1422,8 +1422,10 @@ static int flb_main_run(int argc, char **argv)
14221422
flb_utils_set_daemon(config);
14231423
#if defined(__FreeBSD__) || defined(__NetBSD__) || \
14241424
defined(__OpenBSD__) || defined(__DragonFly__)
1425-
if (flb_create_event_loop(ctx) != 0)
1425+
if (flb_create_event_loop(ctx) != 0) {
1426+
flb_error("[daemon] failed to recreate event loop after daemonizing");
14261427
flb_utils_error(FLB_ERR_CFG_FLUSH);
1428+
}
14271429
#endif
14281430
}
14291431
#endif

0 commit comments

Comments
 (0)