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

messageFlagsAdd deadlocks/freezes if fetch is used #110

Closed
PRR24 opened this issue Feb 1, 2023 · 2 comments
Closed

messageFlagsAdd deadlocks/freezes if fetch is used #110

PRR24 opened this issue Feb 1, 2023 · 2 comments

Comments

@PRR24
Copy link

PRR24 commented Feb 1, 2023

Describe the bug
messageFlagsAdd does not return if fetch was used on the client earlier

To Reproduce

await client.connect();
let lock = await client.getMailboxLock("INBOX");
try {
    let processed: number[] = [];
    for await (let msg of client.fetch({ seen: false }, { source: { maxLength: 2 * 1024 * 1024 } })) {
        console.log(msg.seq);
        processed.push(msg.seq);
    }
    if (processed.length) {
        await client.messageFlagsAdd(processed, ["\\Seen"]); // Freezes without any error/log
    }
// ...

If I take the seq logged to the console and use the solution without fetch:

await client.connect();
let lock = await client.getMailboxLock("INBOX");
try {
    let processed: number[] = [4517];
    if (processed.length) {
        await client.messageFlagsAdd(processed, ["\\Seen"]); // Works perfectly
    }

everything works as expected.

Ubuntu 22.04, imapflow 1.0.119

@PRR24
Copy link
Author

PRR24 commented Feb 1, 2023

Made a mistake while oversimplifying the repro case.
The problem occurs when fetch() is not fully processed, eg either with:

await client.connect();
let lock = await client.getMailboxLock("INBOX");
try {
    let processed: number[] = [];
    for await (let msg of client.fetch({ seen: false }, { source: { maxLength: 2 * 1024 * 1024 } })) {
        console.log(msg.seq);
        processed.push(msg.seq);
        break; // Important for repro
    }
    if (processed.length) {
        await client.messageFlagsAdd(processed, ["\\Seen"]); // Freezes without any error/log
    }
// ...

Or with (the original code I started to track the problem down from):

await client.connect();
let lock = await client.getMailboxLock("INBOX");
try {
    for await (let msg of client.fetch({ seen: false }, { source: { maxLength: 2 * 1024 * 1024 } })) {
        console.log(msg.seq);
        client.messageFlagsAdd({ seq: msg.seq }, ["\\Seen"]); // Freezes without any error/log
    }
// ...

Just for future record, what is the recommended way to safely cancel the fetch loop?

@andris9
Copy link
Collaborator

andris9 commented Feb 1, 2023

Oh, I guess I haven't documented it well enough (if at all 🤔). You can not run any IMAP command before the previous one has finished. So you can't run messageFlagsAdd inside the fetch loop. This is because you can only process a single IMAP command at a time (can't do anything in parallel in the same session), and the fetch command has not finished yet. For other commands, it's not a big issue, as ImapFlow just pipelines these commands, but the fetch loop is special – it allows you to run your code while the command is still being executed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants