Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: Synchronize autoevent start and stop operation to prevent fatal segmentation faults in device services #444

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions src/c/autoevent.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

#include <microhttpd.h>

static int init_flg_mstr = -1;
static int init_flg_slv = -1;

typedef struct edgex_autoimpl
{
devsdk_service_t *svc;
Expand All @@ -45,6 +48,7 @@ static void edgex_autoimpl_release (edgex_autoimpl *ai)

static void *ae_runner (void *p)
{
sem_wait (&slv_mutex);
edgex_autoimpl *ai = (edgex_autoimpl *)p;
atomic_fetch_add (&ai->refs, 1);

Expand All @@ -55,6 +59,7 @@ static void *ae_runner (void *p)
{
edgex_device_release (ai->svc, dev);
edgex_autoimpl_release (ai);
sem_post (&slv_mutex);
return NULL;
}
edgex_device_alloc_crlid (NULL);
Expand Down Expand Up @@ -138,6 +143,7 @@ static void *ae_runner (void *p)
}
}
edgex_autoimpl_release (ai);
sem_post (&slv_mutex);
return NULL;
}

Expand All @@ -154,6 +160,17 @@ static void *starter (void *p)

void edgex_device_autoevent_start (devsdk_service_t *svc, edgex_device *dev)
{
if (init_flg_mstr != 0)
{
init_flg_mstr = sem_init (&mstr_mutex, 0, 1);
}

if (init_flg_slv != 0)
{
init_flg_slv = sem_init (&slv_mutex, 0, 1)
}

sem_wait (&mstr_mutex);
for (edgex_device_autoevents *ae = dev->autos; ae; ae = ae->next)
{
if (ae->impl == NULL)
Expand Down Expand Up @@ -202,6 +219,7 @@ void edgex_device_autoevent_start (devsdk_service_t *svc, edgex_device *dev)
iot_schedule_add (ae->impl->svc->scheduler, ae->impl->handle);
}
}
sem_post (&mstr_mutex);
}

static void stopper (edgex_autoimpl *ai)
Expand Down Expand Up @@ -230,3 +248,44 @@ void edgex_device_autoevent_stop (edgex_device *dev)
}
}
}

void edgex_device_autoevent_stop_to_update (edgex_device *dev)
{
if (init_flg_mstr != 0)
{
init_flg_mstr = sem_init (&mstr_mutex, 0, 1);
}

if (init_flg_slv != 0)
{
init_flg_slv = sem_init (&slv_mutex, 0, 1);
}

sem_wait (&mstr_mutex);
for (edgex_device_autoevents *ae = dev->autos; ae; ae = ae->next)
{
if (ae->impl)
{
sem_wait (&slv_mutex);
stopper (ae->impl);
ae->impl = NULL;
sem_post (&slv_mutex);
}
}
sem_post (&mstr_mutex);
}

void clear_ae_signals ()
{
if (init_flg_mstr == 0)
{
sem_destroy (&mstr_mutex);
init_flg_mstr = -1;
}

if (init_flg_slv == 0)
{
sem_destroy (&slv_mutex);
init_flg_slv = -1;
}
}
9 changes: 9 additions & 0 deletions src/c/autoevent.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,23 @@
* SPDX-License-Identifier: Apache-2.0
*
*/
#include <semaphore.h>

#ifndef _EDGEX_DEVICE_AUTOEVENT_H
#define _EDGEX_DEVICE_AUTOEVENT_H 1

#include "service.h"

sem_t mstr_mutex;

sem_t slv_mutex;

void edgex_device_autoevent_start (devsdk_service_t *svc, edgex_device *dev);

void edgex_device_autoevent_stop (edgex_device *dev);

void edgex_device_autoevent_stop_to_update (edgex_device *dev);

void clear_ae_signals ();

#endif
3 changes: 2 additions & 1 deletion src/c/devmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ void edgex_devmap_clear (edgex_devmap_t *map)
edgex_map_remove (&map->devices, key);
key = next;
}
clear_ae_signals ();
pthread_rwlock_unlock (&map->lock);
}

Expand Down Expand Up @@ -348,7 +349,7 @@ void edgex_devmap_update_profile (devsdk_service_t *svc, edgex_deviceprofile *dp
edgex_device **dev = edgex_map_get (&svc->devices->devices, key);
if ((*dev)->profile == old)
{
edgex_device_autoevent_stop (*dev);
edgex_device_autoevent_stop_to_update (*dev);
(*dev)->profile = dp;
edgex_device_autoevent_start (svc, *dev);
}
Expand Down