Files
Minixer/Source/Plugin/PluginHostLauncher.cpp
XmengMD a9bbf9771f 修复插件槽加载/卸载交互缺陷,并解决删除插件、加载预设阻塞界面
问题
1. 插件加载中只在底部状态栏提示 Loading,槽位自身没有加载态;且加载中仍可再次点开
   load plugin,可对同一槽位重复发起加载。
2. 删除插件会阻塞界面(大型插件约 2 秒),加载 .minixer 预设时更严重——预设里 N 个
   插件会导致数秒至十几秒完全无响应;而加载单个插件却不卡。

根因
- 槽位只有“有/无插件”两种显示状态,而加载期间 pluginSlotNodes 必为 nullptr,被误判
  成空槽,于是所有空槽分支(可点击加载、显示 “+”、空槽右键菜单、可拖拽、不可复制)
  全部串错。
- 删除阻塞的根因不在槽位层:AudioProcessorGraph 的渲染序列
  GraphRenderSequence::NodeOp 持有 Node::Ptr 强引用(juce_AudioProcessorGraph.cpp:889),
  节点从图中摘除后仍被旧渲染序列保活;而 RenderSequenceExchange::set() 与其 500ms
  timerCallback() 都在消息线程销毁被淘汰的序列,因此
  ~PluginBridgeNode() -> shutdown() -> launcher->waitForExit(2000) 依旧在消息线程执行。
  预设加载会一次摘除 N 个节点、旧序列一次性析构,故冻结 N×2s。

修改
一、槽位进行中状态(PluginSlotComponent / ChannelStripComponent / MainComponent)
- 新增 PluginSlotBusyState{none, loading, removing} 取代原 bool loading;槽位自身显示
  “Loading xxx…/Removing xxx…”,加淡蓝底 + 强调色描边,进行中屏蔽加载、打开编辑器、
  拖拽、旁通等交互。
- 加载中右侧按钮改为“取消加载”(新增 pluginSlotLoadCancelRequested 回调),右键仅提供
  Cancel loading;卸载中不可取消,右键仅提示当前状态。
- 替换插件时不再先移除旧插件:加载期间旧插件继续工作,待新插件在后台就绪后再原子替换;
  加载失败或取消时保留原插件,不再出现替换瞬间断音与槽位清空。
- “卸载中”状态改由子进程真正退出后的回调解除,状态栏同步从 Removing… 更新为
  Slot N cleared(仅用户主动删除时回写,预设批量清空不刷屏)。

二、删除/替换/预设加载/退出不再阻塞(PluginHostLauncher / PluginBridgeNode)
- 新增 PluginHostProcessReaper:把子进程交给后台线程池完成“等待退出(宽限 3s)→ 超时
  强杀”,绝不在调用线程上等待;reapAsync 支持完成后在消息线程回调。
- PluginBridgeNode::shutdown() 不再同步 waitForExit,改为把 launcher 交给收割器;新增
  setShutdownCompletionCallback 供槽位解除“卸载中”状态。
- PluginHostLauncher 析构时强杀仍在运行的子进程(juce::ChildProcess 析构并不会结束进程),
  避免残留孤儿进程;程序退出时调用 drain(2000) 清场。

三、其他同源问题
- 槽位内容整体移位时,取消移位区间内所有在途加载,避免加载完成后写回错位槽位并产生
  孤立节点。
- 空槽与进行中的槽位不再可拖拽(mouseDrag 与 isInterestedInDragSource 双重判断)。
- 复制空槽/进行中槽位给出状态栏提示,不再静默无反馈。
- 跨实例粘贴改为按最后一个 ':' 拆分,插件名含 ':' 不再被截断。
- 右键 Move down 的硬编码槽位数 11 改为按实际槽位数判断。
2026-09-24 21:34:44 +08:00

199 lines
5.9 KiB
C++

/*
==============================================================================
PluginHostLauncher.cpp
==============================================================================
*/
#include "PluginHostLauncher.h"
#include <algorithm>
#include <memory>
#include <mutex>
#include <vector>
namespace minixer
{
//==============================================================================
PluginHostLauncher::~PluginHostLauncher()
{
// 进程句柄析构不会结束子进程,这里显式强杀,避免残留孤儿 PluginHost 进程。
// 正常情况下(已由收割器等待退出)子进程已不在运行,此处为无操作。
if (process.isRunning())
process.kill();
}
//==============================================================================
juce::File PluginHostLauncher::getHostExecutableForArchitecture (PluginArchitecture arch)
{
auto hostExe = juce::File::getSpecialLocation (juce::File::currentApplicationFile);
const juce::String baseName = "PluginHost";
juce::String fileName;
switch (arch)
{
case PluginArchitecture::x86:
fileName = baseName + "32.exe";
break;
case PluginArchitecture::x64:
fileName = baseName + "64.exe";
break;
default:
fileName = baseName + "64.exe";
break;
}
return hostExe.getParentDirectory().getChildFile (fileName);
}
//==============================================================================
bool PluginHostLauncher::launch (const PluginHostLaunchOptions& options)
{
auto executable = getHostExecutableForArchitecture (options.architecture);
if (! executable.existsAsFile())
{
lastError = "PluginHost executable not found: " + executable.getFullPathName();
return false;
}
juce::StringArray args;
args.add (executable.getFullPathName());
args.add ("--mode=" + options.mode);
args.add ("--plugin-id=" + options.pluginId);
args.add ("--plugin-path=" + options.pluginPath);
args.add ("--ipc-key=" + options.ipcKey);
args.add ("--max-frames=" + juce::String (static_cast<juce::int64> (options.maxFramesPerBlock)));
if (options.logPath.isNotEmpty())
args.add ("--log-path=" + options.logPath);
if (options.pluginDescriptionXmlB64.isNotEmpty())
args.add ("--plugin-desc-b64=" + options.pluginDescriptionXmlB64);
exitCodeKnown = false;
knownExitCode = 0;
if (! process.start (args, juce::ChildProcess::wantStdOut | juce::ChildProcess::wantStdErr))
{
lastError = "Failed to start PluginHost process";
return false;
}
return true;
}
//==============================================================================
bool PluginHostLauncher::isRunning() const
{
return process.isRunning();
}
//==============================================================================
bool PluginHostLauncher::waitForExit (int timeoutMs) const
{
return process.waitForProcessToFinish (timeoutMs);
}
//==============================================================================
void PluginHostLauncher::terminateProcess()
{
process.kill();
}
//==============================================================================
int PluginHostLauncher::getExitCode() const
{
if (exitCodeKnown)
return knownExitCode;
const uint32_t code = process.getExitCode();
exitCodeKnown = true;
knownExitCode = static_cast<int> (code);
return knownExitCode;
}
//==============================================================================
bool PluginHostLauncher::didCrash() const
{
if (process.isRunning())
return false;
return getExitCode() != 0;
}
//==============================================================================
PluginHostProcessReaper& PluginHostProcessReaper::getInstance()
{
static PluginHostProcessReaper instance;
return instance;
}
//==============================================================================
void PluginHostProcessReaper::reapAsync (std::shared_ptr<PluginHostLauncher> launcher,
std::function<void()> onFinished)
{
if (launcher == nullptr)
{
// 没有子进程可等待:直接回调,避免调用方一直停留在“卸载中”
if (onFinished != nullptr)
juce::MessageManager::callAsync (std::move (onFinished));
return;
}
{
std::lock_guard<std::mutex> lock (mutex);
inFlight.push_back (launcher);
}
auto task = [this, launcher, onFinished]() -> juce::ThreadPoolJob::JobStatus
{
// 后台线程:等待子进程优雅退出,超时则强杀(绝不在消息线程上等待)
if (launcher->isRunning())
{
if (! launcher->waitForExit (exitGraceMs))
launcher->terminateProcess();
}
{
std::lock_guard<std::mutex> lock (mutex);
inFlight.erase (std::remove (inFlight.begin(), inFlight.end(), launcher), inFlight.end());
}
if (onFinished != nullptr)
juce::MessageManager::callAsync (onFinished);
return juce::ThreadPoolJob::jobHasFinished;
};
pool.addJob (std::function<juce::ThreadPoolJob::JobStatus()> (std::move (task)));
}
//==============================================================================
void PluginHostProcessReaper::drain (int timeoutMs)
{
// 先给在途收割一点时间让子进程优雅退出
pool.removeAllJobs (true, timeoutMs);
// 兜底:仍未结束的(含已被移出任务队列的)子进程直接强杀,避免残留孤儿进程
std::vector<std::shared_ptr<PluginHostLauncher>> remaining;
{
std::lock_guard<std::mutex> lock (mutex);
remaining.swap (inFlight);
}
for (auto& launcher : remaining)
{
if (launcher != nullptr && launcher->isRunning())
launcher->terminateProcess();
}
}
} // namespace minixer