Fix: Replace MSG_NOSIGNAL (Linux-only) with macOS-compatible send() in HelperConnection.cpp and HelperIPC.cpp
Browse files
Source/Plugin/HelperConnection.cpp
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
#include "HelperConnection.h"
|
| 2 |
-
#include "
|
| 3 |
|
| 4 |
#include <sys/socket.h>
|
| 5 |
#include <sys/un.h>
|
|
@@ -17,6 +17,8 @@ using json = nlohmann::json;
|
|
| 17 |
HelperConnection::HelperConnection()
|
| 18 |
{
|
| 19 |
myPid = getpid();
|
|
|
|
|
|
|
| 20 |
}
|
| 21 |
|
| 22 |
HelperConnection::~HelperConnection()
|
|
@@ -26,7 +28,7 @@ HelperConnection::~HelperConnection()
|
|
| 26 |
|
| 27 |
bool HelperConnection::spawnHelper()
|
| 28 |
{
|
| 29 |
-
killHelper();
|
| 30 |
|
| 31 |
// 1. Create shared memory
|
| 32 |
auto shmName = IPC::getShmName(myPid);
|
|
@@ -46,7 +48,7 @@ bool HelperConnection::spawnHelper()
|
|
| 46 |
|
| 47 |
// 3. Create Unix domain socket (server side)
|
| 48 |
auto socketPath = IPC::getSocketPath(myPid);
|
| 49 |
-
unlink(socketPath.c_str());
|
| 50 |
|
| 51 |
serverSocketFd = socket(AF_UNIX, SOCK_STREAM, 0);
|
| 52 |
if (serverSocketFd < 0)
|
|
@@ -69,7 +71,7 @@ bool HelperConnection::spawnHelper()
|
|
| 69 |
return false;
|
| 70 |
}
|
| 71 |
|
| 72 |
-
// 4. Find helper binary
|
| 73 |
auto helperPath = juce::File::getSpecialLocation(juce::File::currentExecutableFile)
|
| 74 |
.getParentDirectory() // MacOS/
|
| 75 |
.getParentDirectory() // Contents/
|
|
@@ -78,7 +80,6 @@ bool HelperConnection::spawnHelper()
|
|
| 78 |
|
| 79 |
if (!helperPath.exists())
|
| 80 |
{
|
| 81 |
-
// Fallback: look next to the plugin binary (for development)
|
| 82 |
helperPath = juce::File::getSpecialLocation(juce::File::currentExecutableFile)
|
| 83 |
.getParentDirectory()
|
| 84 |
.getChildFile(PluginBridgeConstants::kHelperBinaryName);
|
|
@@ -135,6 +136,10 @@ bool HelperConnection::spawnHelper()
|
|
| 135 |
return false;
|
| 136 |
}
|
| 137 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
// 7. Start IPC listen thread
|
| 139 |
ipcRunning.store(true);
|
| 140 |
ipcThread = std::thread([this]() { ipcListenLoop(); });
|
|
@@ -142,7 +147,7 @@ bool HelperConnection::spawnHelper()
|
|
| 142 |
// 8. Start crash detection timer
|
| 143 |
startTimerHz(4);
|
| 144 |
|
| 145 |
-
// Wait for "ready" notification
|
| 146 |
int waitMs = 0;
|
| 147 |
while (state.load() == State::Starting && waitMs < IPC::kHelperTimeoutMs)
|
| 148 |
{
|
|
@@ -157,33 +162,28 @@ void HelperConnection::killHelper()
|
|
| 157 |
{
|
| 158 |
stopTimer();
|
| 159 |
|
| 160 |
-
// Stop IPC
|
| 161 |
ipcRunning.store(false);
|
| 162 |
if (clientSocketFd >= 0) { close(clientSocketFd); clientSocketFd = -1; }
|
| 163 |
if (serverSocketFd >= 0) { close(serverSocketFd); serverSocketFd = -1; }
|
| 164 |
if (ipcThread.joinable()) ipcThread.join();
|
| 165 |
|
| 166 |
-
// Kill helper process
|
| 167 |
if (helperPid > 0 && helperRunning.load())
|
| 168 |
{
|
| 169 |
kill(helperPid, SIGTERM);
|
| 170 |
int status;
|
| 171 |
waitpid(helperPid, &status, WNOHANG);
|
| 172 |
-
usleep(100000);
|
| 173 |
-
kill(helperPid, SIGKILL);
|
| 174 |
waitpid(helperPid, &status, 0);
|
| 175 |
}
|
| 176 |
helperPid = 0;
|
| 177 |
helperRunning.store(false);
|
| 178 |
|
| 179 |
-
// Clean up shared resources
|
| 180 |
sharedAudio.destroy();
|
| 181 |
inputSem.destroy();
|
| 182 |
outputSem.destroy();
|
| 183 |
|
| 184 |
-
// Remove socket file
|
| 185 |
unlink(IPC::getSocketPath(myPid).c_str());
|
| 186 |
-
|
| 187 |
state.store(State::Disconnected);
|
| 188 |
}
|
| 189 |
|
|
@@ -194,12 +194,10 @@ std::string HelperConnection::sendCommand(const std::string& jsonCmd)
|
|
| 194 |
if (clientSocketFd < 0 || !helperRunning.load())
|
| 195 |
return "{\"ok\":false,\"error\":\"not connected\"}";
|
| 196 |
|
| 197 |
-
// Send
|
| 198 |
auto data = jsonCmd + "\n";
|
| 199 |
-
if (::send(clientSocketFd, data.c_str(), data.size(),
|
| 200 |
return "{\"ok\":false,\"error\":\"send failed\"}";
|
| 201 |
|
| 202 |
-
// Wait for response (up to 5s)
|
| 203 |
responseReady.store(false);
|
| 204 |
int waitMs = 0;
|
| 205 |
while (!responseReady.load() && waitMs < 5000)
|
|
@@ -218,17 +216,14 @@ void HelperConnection::timerCallback()
|
|
| 218 |
{
|
| 219 |
if (helperPid <= 0) return;
|
| 220 |
|
| 221 |
-
// Check if helper is still alive
|
| 222 |
int status;
|
| 223 |
pid_t result = waitpid(helperPid, &status, WNOHANG);
|
| 224 |
|
| 225 |
if (result == helperPid)
|
| 226 |
{
|
| 227 |
-
// Helper has exited
|
| 228 |
helperRunning.store(false);
|
| 229 |
state.store(State::Crashed);
|
| 230 |
stopTimer();
|
| 231 |
-
|
| 232 |
DBG("HelperConnection: Helper process died");
|
| 233 |
if (onHelperCrashed)
|
| 234 |
onHelperCrashed();
|
|
@@ -273,17 +268,13 @@ void HelperConnection::ipcListenLoop()
|
|
| 273 |
|
| 274 |
if (msg.empty()) continue;
|
| 275 |
|
| 276 |
-
// Check if it's a notification or a command response
|
| 277 |
try
|
| 278 |
{
|
| 279 |
auto j = json::parse(msg);
|
| 280 |
if (j.contains("notify"))
|
| 281 |
-
{
|
| 282 |
handleNotification(msg);
|
| 283 |
-
}
|
| 284 |
else
|
| 285 |
{
|
| 286 |
-
// It's a response to our command
|
| 287 |
lastResponse = msg;
|
| 288 |
responseReady.store(true);
|
| 289 |
}
|
|
|
|
| 1 |
#include "HelperConnection.h"
|
| 2 |
+
#include "Constants.h"
|
| 3 |
|
| 4 |
#include <sys/socket.h>
|
| 5 |
#include <sys/un.h>
|
|
|
|
| 17 |
HelperConnection::HelperConnection()
|
| 18 |
{
|
| 19 |
myPid = getpid();
|
| 20 |
+
// Ignore SIGPIPE globally (macOS doesn't have MSG_NOSIGNAL)
|
| 21 |
+
signal(SIGPIPE, SIG_IGN);
|
| 22 |
}
|
| 23 |
|
| 24 |
HelperConnection::~HelperConnection()
|
|
|
|
| 28 |
|
| 29 |
bool HelperConnection::spawnHelper()
|
| 30 |
{
|
| 31 |
+
killHelper();
|
| 32 |
|
| 33 |
// 1. Create shared memory
|
| 34 |
auto shmName = IPC::getShmName(myPid);
|
|
|
|
| 48 |
|
| 49 |
// 3. Create Unix domain socket (server side)
|
| 50 |
auto socketPath = IPC::getSocketPath(myPid);
|
| 51 |
+
unlink(socketPath.c_str());
|
| 52 |
|
| 53 |
serverSocketFd = socket(AF_UNIX, SOCK_STREAM, 0);
|
| 54 |
if (serverSocketFd < 0)
|
|
|
|
| 71 |
return false;
|
| 72 |
}
|
| 73 |
|
| 74 |
+
// 4. Find helper binary
|
| 75 |
auto helperPath = juce::File::getSpecialLocation(juce::File::currentExecutableFile)
|
| 76 |
.getParentDirectory() // MacOS/
|
| 77 |
.getParentDirectory() // Contents/
|
|
|
|
| 80 |
|
| 81 |
if (!helperPath.exists())
|
| 82 |
{
|
|
|
|
| 83 |
helperPath = juce::File::getSpecialLocation(juce::File::currentExecutableFile)
|
| 84 |
.getParentDirectory()
|
| 85 |
.getChildFile(PluginBridgeConstants::kHelperBinaryName);
|
|
|
|
| 136 |
return false;
|
| 137 |
}
|
| 138 |
|
| 139 |
+
// Set SO_NOSIGPIPE on the accepted socket (macOS)
|
| 140 |
+
int nosigpipe = 1;
|
| 141 |
+
setsockopt(clientSocketFd, SOL_SOCKET, SO_NOSIGPIPE, &nosigpipe, sizeof(nosigpipe));
|
| 142 |
+
|
| 143 |
// 7. Start IPC listen thread
|
| 144 |
ipcRunning.store(true);
|
| 145 |
ipcThread = std::thread([this]() { ipcListenLoop(); });
|
|
|
|
| 147 |
// 8. Start crash detection timer
|
| 148 |
startTimerHz(4);
|
| 149 |
|
| 150 |
+
// Wait for "ready" notification
|
| 151 |
int waitMs = 0;
|
| 152 |
while (state.load() == State::Starting && waitMs < IPC::kHelperTimeoutMs)
|
| 153 |
{
|
|
|
|
| 162 |
{
|
| 163 |
stopTimer();
|
| 164 |
|
|
|
|
| 165 |
ipcRunning.store(false);
|
| 166 |
if (clientSocketFd >= 0) { close(clientSocketFd); clientSocketFd = -1; }
|
| 167 |
if (serverSocketFd >= 0) { close(serverSocketFd); serverSocketFd = -1; }
|
| 168 |
if (ipcThread.joinable()) ipcThread.join();
|
| 169 |
|
|
|
|
| 170 |
if (helperPid > 0 && helperRunning.load())
|
| 171 |
{
|
| 172 |
kill(helperPid, SIGTERM);
|
| 173 |
int status;
|
| 174 |
waitpid(helperPid, &status, WNOHANG);
|
| 175 |
+
usleep(100000);
|
| 176 |
+
kill(helperPid, SIGKILL);
|
| 177 |
waitpid(helperPid, &status, 0);
|
| 178 |
}
|
| 179 |
helperPid = 0;
|
| 180 |
helperRunning.store(false);
|
| 181 |
|
|
|
|
| 182 |
sharedAudio.destroy();
|
| 183 |
inputSem.destroy();
|
| 184 |
outputSem.destroy();
|
| 185 |
|
|
|
|
| 186 |
unlink(IPC::getSocketPath(myPid).c_str());
|
|
|
|
| 187 |
state.store(State::Disconnected);
|
| 188 |
}
|
| 189 |
|
|
|
|
| 194 |
if (clientSocketFd < 0 || !helperRunning.load())
|
| 195 |
return "{\"ok\":false,\"error\":\"not connected\"}";
|
| 196 |
|
|
|
|
| 197 |
auto data = jsonCmd + "\n";
|
| 198 |
+
if (::send(clientSocketFd, data.c_str(), data.size(), 0) < 0)
|
| 199 |
return "{\"ok\":false,\"error\":\"send failed\"}";
|
| 200 |
|
|
|
|
| 201 |
responseReady.store(false);
|
| 202 |
int waitMs = 0;
|
| 203 |
while (!responseReady.load() && waitMs < 5000)
|
|
|
|
| 216 |
{
|
| 217 |
if (helperPid <= 0) return;
|
| 218 |
|
|
|
|
| 219 |
int status;
|
| 220 |
pid_t result = waitpid(helperPid, &status, WNOHANG);
|
| 221 |
|
| 222 |
if (result == helperPid)
|
| 223 |
{
|
|
|
|
| 224 |
helperRunning.store(false);
|
| 225 |
state.store(State::Crashed);
|
| 226 |
stopTimer();
|
|
|
|
| 227 |
DBG("HelperConnection: Helper process died");
|
| 228 |
if (onHelperCrashed)
|
| 229 |
onHelperCrashed();
|
|
|
|
| 268 |
|
| 269 |
if (msg.empty()) continue;
|
| 270 |
|
|
|
|
| 271 |
try
|
| 272 |
{
|
| 273 |
auto j = json::parse(msg);
|
| 274 |
if (j.contains("notify"))
|
|
|
|
| 275 |
handleNotification(msg);
|
|
|
|
| 276 |
else
|
| 277 |
{
|
|
|
|
| 278 |
lastResponse = msg;
|
| 279 |
responseReady.store(true);
|
| 280 |
}
|