Richienb / audic

Play some audio.
MIT License
68 stars 9 forks source link

RequestError: connect ECONNREFUSED #18

Open nolanbarry opened 1 year ago

nolanbarry commented 1 year ago

Occasionally when playing a sound I get the following error:

.../node_modules/got/dist/source/core/index.js:970                                                                      [92/162]
            error = error instanceof timed_out_1.TimeoutError ? new TimeoutError(error, this.timings, this) : new RequestError(error.message, error, this);                                           
                                                                                                              ^                                                                                       
RequestError: connect ECONNREFUSED 10.37.58.128:49457                                                                                                                                                 
    at ClientRequest.<anonymous> (.../node_modules/got/dist/source/core/index.js:970:111)                                       
    at Object.onceWrapper (node:events:628:26)                                                                                                                                                        
    at ClientRequest.emit (node:events:525:35)                                                                                                                                                        
    at ClientRequest.emit (node:domain:489:12)                                                                                                                                                        
    at ClientRequest.origin.emit (.../node_modules/@szmarczak/http-timer/dist/source/index.js:43:20)                            
    at Socket.socketErrorListener (node:_http_client:488:9)                                                                                                                                           
    at Socket.emit (node:events:513:28)                                                                                                                                                               
    at Socket.emit (node:domain:489:12)                                                                                                                                                               
    at emitErrorNT (node:internal/streams/destroy:151:8)                                                                                                                                              
    at emitErrorCloseNT (node:internal/streams/destroy:116:3)                                                                                                                                         
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1283:16) {                                                                                                                               
  code: 'ECONNREFUSED',                                                                                                                                                                               
  timings: {                                                                                                                                                                                          
    start: 1670520647725,                                                                                                                                                                             
    socket: 1670520647725,                                                                                                                                                                            
    lookup: undefined,                                                                                                                                                                                
    connect: undefined,                                                                                                                                                                               
    secureConnect: undefined,                                                                                                                                                                         
    upload: undefined,                                                                                                                                                                                
    response: undefined,                                                                                                                                                                              
    end: undefined,                              
    error: 1670520647725,                        
    abort: undefined,                            
    phases: {                                    
      wait: 0,                                   
      dns: undefined,                            
      tcp: undefined,                            
      tls: undefined,                            
      request: undefined,                        
      firstByte: undefined,                      
      download: undefined,                       
      total: 0         
    }                                            
  }                                              
}   

Running vlc in the command line (even vlc --help, which doesn't keep a process running in the foreground) and restarting the node process seems to fix it, but I'm wondering if there's a little more insight on what exactly is happening behind the scenes?

AbdBarho commented 1 year ago

I managed to reproduce reliably on my machine with this snippet, not sure if it is only my machine or what:

import { playAudioFile } from "audic";

playAudioFile("not.mp3");

setTimeout(() => {
  playAudioFile("not.mp3");
}, 5000);

The audio file is only 3 seconds

Richienb commented 1 year ago

The issue probably resides here: https://github.com/Richienb/vlc/blob/45eedbf9bab5e8985e6b32f1d9217c3e8a368c0a/source/index.ts#L190 A possible cause could be that it tries to connect to vlc while it is still starting the server so it fails.

AbdBarho commented 1 year ago

@Richienb using your vlc library directly solves the problem for me (ignore ugly nesting):


import createVlc from "@richienb/vlc";

const vlc = await createVlc();

const play = () => vlc.command("in_play", { input: "not.mp3" });

play();

setTimeout(() => {
  play();
  setTimeout(() => {
    vlc.kill();
  }, 5000);
}, 5000);
Richienb commented 1 year ago

The first setTimeout in your example supports the hypothesis that the http request is being made too early in the codebase of audic. Not sure how to fix it well, PR welcome.

nolanbarry commented 1 year ago

I ended up creating my own VLC wrapper (needed video playback anyway), which uses a request queue to send requests only after a successful ping to VLC. That'd require changing how this library works in a big way so ditto on the no easy fix. Could perhaps just make the constructor private and add a static async function that returns the VLC instance once it's established that it is accepting connections?

Richienb commented 1 year ago

The solution is to probably change the upstream library https://github.com/Richienb/vlc instead.