From d747ac66e687db828f7dd9d0ff8a99ce1f802698 Mon Sep 17 00:00:00 2001 From: Menci Date: Wed, 20 Mar 2019 20:17:01 +0800 Subject: [PATCH 1/3] Workaround a race condition in submitting --- modules/problem.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/modules/problem.js b/modules/problem.js index afa41dc..84df7e2 100644 --- a/modules/problem.js +++ b/modules/problem.js @@ -683,7 +683,9 @@ app.post('/problem/:id/submit', app.multer.fields([{ name: 'answer', maxCount: 1 code: formatted }); - await formattedCode.save(); + try { + await formattedCode.save(); + } catch (e) {} } } } From 3ae5262797efe0ac607bbb712f727072bf744ce6 Mon Sep 17 00:00:00 2001 From: Menci Date: Wed, 20 Mar 2019 20:21:42 +0800 Subject: [PATCH 2/3] Workaround Sequelize's .save() possibly won't resolve bug --- models/common.js | 3 ++- utility.js | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/models/common.js b/models/common.js index 952e1a1..7e1342f 100644 --- a/models/common.js +++ b/models/common.js @@ -34,7 +34,8 @@ class Model { for (let key in obj) this.record.set(key, obj[key]); let isNew = this.record.isNewRecord; - await this.record.save(); + + await syzoj.utils.withTimeoutRetry(() => this.record.save()); if (!isNew) return; await this.reload(); diff --git a/utility.js b/utility.js index 54eec06..21936c2 100644 --- a/utility.js +++ b/utility.js @@ -294,5 +294,18 @@ module.exports = { async saveConfig() { let fs = require('fs-extra'); fs.writeFileAsync(syzoj.configDir, JSON.stringify(syzoj.config, null, 2)); + }, + withTimeoutRetry(func) { + let attemptCount = 0; + return new Promise((resolve, reject) => { + function attempt() { + if (attemptCount++) console.log(`syzoj.utils.withTimeout(): attemptCount = ${attemptCount}`); + Promise.method(func)().timeout(5000) + .then(resolve) + .catch(Promise.TimeoutError, attempt) + .catch(reject); + } + attempt(); + }); } }; From dd132158f223397e8cf764a2a5a36e9dbddf67c2 Mon Sep 17 00:00:00 2001 From: Menci Date: Wed, 20 Mar 2019 20:28:00 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E8=AE=A9=E6=88=91=E5=BA=B7=E5=BA=B7?= =?UTF-8?q?=E6=98=AF=E5=93=AA=E4=B8=AA=E5=B0=8F=E5=8F=AF=E7=88=B1=E4=B8=8D?= =?UTF-8?q?=E5=A5=BD=E5=A5=BD=E7=9C=8B=E6=95=99=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app.js b/app.js index 01c6dcf..a8eb5bd 100644 --- a/app.js +++ b/app.js @@ -22,6 +22,14 @@ global.syzoj = { console.log(obj); }, async run() { + // Check config + if (syzoj.config.session_secret === '@SESSION_SECRET@' + || syzoj.config.email_jwt_secret === '@EMAIL_JWT_SECRET@' + || syzoj.config.db.password === '@DATABASE_PASSWORD@') { + console.log('Please generate and fill the secrets in config!'); + process.exit(); + } + let Express = require('express'); global.app = Express();