Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(processor): remove race condition failsafe #4791

Merged
merged 3 commits into from
Oct 8, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/plugins/processor/asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const Promise = require('bluebird');
const { parse: yfm } = require('hexo-front-matter');
const { extname, relative } = require('path');
const { Pattern } = require('hexo-util');
const { magenta } = require('nanocolors');

module.exports = ctx => {
return {
Expand Down Expand Up @@ -96,11 +97,10 @@ function processPage(ctx, file) {
data.layout = false;
}

// FIXME: Data may be inserted when reading files. Load it again to prevent
// race condition. We have to solve this in warehouse.
const doc = Page.findOne({source: path});

if (doc) {
if (file.type !== 'update') {
ctx.log.warn(`Trying to "create" ${magenta(file.path)}, but the file already exists!`);
}
return doc.replace(data);
}

Expand Down
8 changes: 4 additions & 4 deletions lib/plugins/processor/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { parse: yfm } = require('hexo-front-matter');
const { extname, join } = require('path');
const { stat, listDir } = require('hexo-fs');
const { slugize, Pattern, Permalink } = require('hexo-util');
const { magenta } = require('nanocolors');

const postDir = '_posts/';
const draftDir = '_drafts/';
Expand Down Expand Up @@ -163,11 +164,10 @@ function processPost(ctx, file) {
data.permalink = undefined;
}

// FIXME: Data may be inserted when reading files. Load it again to prevent
// race condition. We have to solve this in warehouse.
const doc = Post.findOne({source: file.path});

if (doc) {
if (file.type !== 'update') {
ctx.log.warn(`Trying to "create" ${magenta(file.path)}, but the file already exists!`);
}
return doc.replace(data);
}
Comment on lines -166 to 172
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are only three reasons that a doc will already exist while the processor is trying to create a post:

  • A race condition inside the warehouse (which seems to be fixed in warehouse@2.1.0 back in 2015)
  • The file.type is update. in this case, there is no need to load the doc again.
  • In some rare situation where two file object is created, with the same id and the same create type (E.g. inside our unit test when we trying to create 2 files under the same file.path. This is not likely to happen in the real-world production). But Hexo can still deal with the issue by replacing doc directly. And I add a warning message for the case, and hopefully, someone who encounters the issue would report it to us.


Expand Down