@@ -143,24 +143,25 @@ void flb_init_env()
143143int 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
182189int 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 ()) {
0 commit comments