Skip to content

Commit 8604e84

Browse files
committed
This should fix bug CORE-6477 : Rare race condition in Plugin Manager could lead to the server crash
1 parent 40e1b3c commit 8604e84

1 file changed

Lines changed: 19 additions & 3 deletions

File tree

src/yvalve/PluginManager.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,8 @@ namespace
522522
int release();
523523

524524
private:
525-
~ConfiguredPlugin();
525+
~ConfiguredPlugin() {}
526+
void destroy();
526527

527528
RefPtr<PluginModule> module;
528529
unsigned int regPlugin;
@@ -685,10 +686,15 @@ namespace
685686

686687
GlobalPtr<PluginsMap> plugins;
687688

688-
ConfiguredPlugin::~ConfiguredPlugin()
689+
void ConfiguredPlugin::destroy()
689690
{
690-
MutexLockGuard g(plugins->mutex, FB_FUNCTION);
691+
// plugins->mutex must be entered by current thread
691692

693+
#ifdef DEV_BUILD
694+
if (!plugins->mutex.tryEnter(FB_FUNCTION))
695+
fb_assert(false);
696+
plugins->mutex.leave();
697+
#endif
692698
if (!destroyingPluginsMap)
693699
{
694700
plugins->remove(MapKey(module->getPlugin(regPlugin).type, plugName));
@@ -733,6 +739,16 @@ namespace
733739

734740
if (x == 0)
735741
{
742+
{ // plugins->mutex scope
743+
MutexLockGuard g(plugins->mutex, FB_FUNCTION);
744+
if (refCounter != 0)
745+
return 1;
746+
747+
destroy();
748+
}
749+
750+
// Must run out of mutex scope to avoid deadlock with PluginManager::threadDetach()
751+
// called when module is unloaded by dtor
736752
delete this;
737753
return 0;
738754
}

0 commit comments

Comments
 (0)