about summary refs log tree commit diff
path: root/streaming
diff options
context:
space:
mode:
authorfuyu <54523771+mfmfuyu@users.noreply.github.com>2020-09-22 22:30:41 +0900
committerGitHub <noreply@github.com>2020-09-22 15:30:41 +0200
commite39d97f7008499bbf91bb0fb3ae2f794c50f40fc (patch)
tree0c771096ee380c74ec70cc65a000e073db08d1d6 /streaming
parentb09810a505b0d7e9daa0dd8e4d2a31e8c3e94230 (diff)
Fix crash streaming process when receive invalid json (#14859)
Diffstat (limited to 'streaming')
-rw-r--r--streaming/index.js21
1 files changed, 19 insertions, 2 deletions
diff --git a/streaming/index.js b/streaming/index.js
index 7072d0bd7..791a26941 100644
--- a/streaming/index.js
+++ b/streaming/index.js
@@ -81,6 +81,19 @@ const redisUrlToClient = (defaultConfig, redisUrl) => {
 
 const numWorkers = +process.env.STREAMING_CLUSTER_NUM || (env === 'development' ? 1 : Math.max(os.cpus().length - 1, 1));
 
+/**
+ * @param {string} json
+ * @return {Object.<string, any>|null}
+ */
+const parseJSON = (json) => {
+  try {
+    return JSON.parse(json);
+  } catch (err) {
+    log.error(err);
+    return null;
+  }
+};
+
 const startMaster = () => {
   if (!process.env.SOCKET && process.env.PORT && isNaN(+process.env.PORT)) {
     log.warn('UNIX domain socket is now supported by using SOCKET. Please migrate from PORT hack.');
@@ -522,7 +535,9 @@ const startWorker = (workerId) => {
     log.verbose(req.requestId, `Starting stream from ${ids.join(', ')} for ${accountId}${streamType}`);
 
     const listener = message => {
-      const { event, payload, queued_at } = JSON.parse(message);
+      const json = parseJSON(message);
+      if (!json) return;
+      const { event, payload, queued_at } = json;
 
       const transmit = () => {
         const now            = new Date().getTime();
@@ -932,7 +947,9 @@ const startWorker = (workerId) => {
     ws.on('error', onEnd);
 
     ws.on('message', data => {
-      const { type, stream, ...params } = JSON.parse(data);
+      const json = parseJSON(data);
+      if (!json) return;
+      const { type, stream, ...params } = json;
 
       if (type === 'subscribe') {
         subscribeWebsocketToChannel(session, firstParam(stream), params);