Skip to content

downloadMedia() blocking issue: add timeout to prevent indefinite hang #3829

@2556227850

Description

@2556227850

Is there an existing issue for this?

  • I have searched the existing issues.

Is this a problem caused by your code, or is it specifically because of the library?

  • I have double-checked my code carefully.

Describe the bug.

When using whatsapp-web.js's downloadMedia() method to download media files, I encountered a problem where the method hangs indefinitely when there is a network issue, an invalid media path, or when WhatsApp servers are unresponsive. The function never returns, causing the background process to be stuck.

Worse, if the user logs out or the client is destroyed while downloadMedia() is hanging, the application crashes.

Expected Behavior

It would be ideal if downloadMedia() supported a timeout option, for example:

const media = await  message.downloadMedia(10000); // 10 second timeout

### Steps to Reproduce the Bug or Issue

1. Login to WhatsApp using whatsapp-web.js.
2. Call `downloadMedia()` to download a large file or a file with network issues.
3. Observe that it hangs indefinitely without returning.
4. Attempt to logout  the process crashes.
```javascript
      .....
      const messages = await chat.fetchMessages({ limit: 'Infinity' }) 
      for (let i = 0; i < messages.length; i++) {
        const message = messages[i]
        if (message.hasMedia) {
           const media = await message.downloadMedia() 
        }
      }
      ....
      await client.destroy()

// error 
D:\Code\wwebjs-server\node_modules\puppeteer-core\lib\cjs\puppeteer\common\Connection.js:329
                error: new Errors_js_1.ProtocolError(),
                       ^

ProtocolError: Protocol error (Runtime.callFunctionOn): Target closed.
    at D:\Code\wwebjs-server\node_modules\puppeteer-core\lib\cjs\puppeteer\common\Connection.js:329:24
    at new Promise (<anonymous>)
    at CDPSessionImpl.send (D:\Code\wwebjs-server\node_modules\puppeteer-core\lib\cjs\puppeteer\common\Connection.js:325:16)
    at ExecutionContext._ExecutionContext_evaluate (D:\Code\wwebjs-server\node_modules\puppeteer-core\lib\cjs\puppeteer\common\ExecutionContext.js:211:46)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
    at async ExecutionContext.evaluate (D:\Code\wwebjs-server\node_modules\puppeteer-core\lib\cjs\puppeteer\common\ExecutionContext.js:107:16)
    at async Message.downloadMedia (D:\Code\wwebjs-server\node_modules\whatsapp-web.js\src\structures\Message.js:450:24)
    at async DownloadMedia (D:\Code\wwebjs-server\src\whatsapp\utils\message.js:51:21)
    at async CreateMessageDB (D:\Code\wwebjs-server\src\whatsapp\utils\db.js:156:19)
    at async SyncAllContactChatHistory (D:\Code\wwebjs-server\src\whatsapp\utils\sync.js:54:9) {
  originalMessage: ''
}

Node.js v22.16.0
[nodemon] app crashed - waiting for file changes before starting...

### WhatsApp Account Type

Standard

### Browser Type

Chrome

### Operation System Type

Windows

### Phone OS Type

其他

### WhatsApp-Web.js Version

1.34.0

### WhatsApp Web Version

2.3000.1027512224

### Node.js Version

Node.js v22.16.0

### Authentication Strategy

LocalAuth

### Additional Context

_No response_

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions