ipfs-inactive/js-ipfs-http-client

Error [ERR_STREAM_DESTROYED]: Cannot call write after a stream was destroyed

drewstone opened this issue · 8 comments

Having odd behavior trying to build a CLI that adds data to IPFS. In my tests, I do not hit this error but when I try to run it directly with the CLI I can never add data to IPFS.

Update with the error, when I run the following, I get the following:

    const ipfs = ipfsClient(multiAddr);
    let results = await ipfs.add(Buffer.from(JSON.stringify(ipfsData)));

Error [ERR_STREAM_DESTROYED]: Cannot call write after a stream was destroyed
    at doWrite (_stream_writable.js:411:19)
    at writeOrBuffer (_stream_writable.js:399:5)
    at PassThrough.Writable.write (_stream_writable.js:299:11)
    at PassThrough.ondata (_stream_readable.js:693:20)
    at PassThrough.emit (events.js:193:15)
    at PassThrough.Readable.read (_stream_readable.js:491:10)
    at flow (_stream_readable.js:957:34)
    at resume_ (_stream_readable.js:938:3)
    at process.internalTickCallback (internal/process/next_tick.js:72:19)
Emitted 'error' event at:
    at errorOrDestroy (internal/streams/destroy.js:98:12)
    at PassThrough.onerror (_stream_readable.js:717:7)
    at PassThrough.emit (events.js:188:13)
    at errorOrDestroy (internal/streams/destroy.js:98:12)
    at onwriteError (_stream_writable.js:430:5)
    at onwrite (_stream_writable.js:461:5)
    at doWrite (_stream_writable.js:411:11)
    at writeOrBuffer (_stream_writable.js:399:5)
    [... lines matching original stack trace ...]
    at process.internalTickCallback (internal/process/next_tick.js:72:19)

Sorry to hear you're having problems - can you share some runnable code that demonstrates the problem?

@drewstone it would also be useful to know if you're communicating with a go-ipfs or js-ipfs node and what version.

I am running this in a CLI using commander, the weird thing is the same functionality works in my mocha tests. I just can't figure out why I get this esoteric error outside of the tests.

I'm communicating with a js-ipfs node. The code I'm running is:

    console.log(`Multiaddr: ${multiAddr}`);
    console.log(`IPFSData: ${JSON.stringify(ipfsData)}`);
    const ipfs = ipfsClient(multiAddr);
    let results = await ipfs.add(Buffer.from(JSON.stringify(ipfsData)));

This returns:

Multiaddr: /ip4/127.0.0.1/tcp/5002
IPFSData: {"cosmosAddress":"0x01","lockingAddr":"bcrt1qlef0hastk3m3j63p9gpxpjjjdaq07dgm77y2tuxjcae0lq02vufq0ltp9r","redeemScript":"02450cb17576a914eae9c4a95a0b54517974206c9978a52cf915280b88ac","locktime":3141,"redeemAddress":"n2w4TSc1U1UAzwgB1nwMUqH6AWhxVx7jKk"}
events.js:173
      throw er; // Unhandled 'error' event
      ^

Error [ERR_STREAM_DESTROYED]: Cannot call write after a stream was destroyed
    at doWrite (_stream_writable.js:411:19)
    at writeOrBuffer (_stream_writable.js:399:5)
    at PassThrough.Writable.write (_stream_writable.js:299:11)
    at PassThrough.ondata (_stream_readable.js:693:20)
    at PassThrough.emit (events.js:193:15)
    at PassThrough.Readable.read (_stream_readable.js:491:10)
    at flow (_stream_readable.js:957:34)
    at resume_ (_stream_readable.js:938:3)
    at process.internalTickCallback (internal/process/next_tick.js:72:19)
Emitted 'error' event at:
    at errorOrDestroy (internal/streams/destroy.js:98:12)
    at PassThrough.onerror (_stream_readable.js:717:7)
    at PassThrough.emit (events.js:188:13)
    at errorOrDestroy (internal/streams/destroy.js:98:12)
    at onwriteError (_stream_writable.js:430:5)
    at onwrite (_stream_writable.js:461:5)
    at doWrite (_stream_writable.js:411:11)
    at writeOrBuffer (_stream_writable.js:399:5)
    [... lines matching original stack trace ...]
    at process.internalTickCallback (internal/process/next_tick.js:72:19)

...and the versions of the client library and IPFS node that is running? Are you sure your IPFS node's HTTP API is running on port 5002?

Yea I'm sure the node is running because everything works as intended in my mocha tests, I'm calling the same functions with the same arguments but in the CLI it hits this weird stream error.

The version of the client is

"ipfs-http-client": "^38.0.0",

The version of the node is:

js-ipfs version: 0.37.1

I can't reproduce so far. I'm using:

const Os = require('os')
const clientVersion = require('./node_modules/ipfs-http-client/package.json').version
const addr = '/ip4/127.0.0.1/tcp/5002'
const ipfs = require('ipfs-http-client')(addr)

async function main () {
  console.log(`platform/${Os.platform}`)
  console.log(`arch/${Os.arch}`)
  console.log(`nodejs/${process.versions.node}`)
  console.log(`js-ipfs-http-client/${clientVersion}`)
  console.log((await ipfs.id()).agentVersion)

  const data = {
    cosmosAddress: '0x01',
    lockingAddr: 'bcrt1qlef0hastk3m3j63p9gpxpjjjdaq07dgm77y2tuxjcae0lq02vufq0ltp9r',
    redeemScript: '02450cb17576a914eae9c4a95a0b54517974206c9978a52cf915280b88ac',
    locktime: 3141,
    redeemAddress: 'n2w4TSc1U1UAzwgB1nwMUqH6AWhxVx7jKk'
  }

  const res = await ipfs.add(Buffer.from(JSON.stringify(data)))
  console.log(res)
}

main()

...and getting:

platform/darwin
arch/x64
nodejs/12.11.0
js-ipfs-http-client/38.2.1
js-ipfs/0.37.1
[
  {
    path: 'QmZTBo8NR9jRjyquz5zMeLA9aR2nMHFCsbK7y4PRkLQzm5',
    hash: 'QmZTBo8NR9jRjyquz5zMeLA9aR2nMHFCsbK7y4PRkLQzm5',
    size: 263
  }
]

@drewstone Can you please run the script and post your output?

Yea it works as expected, even when I wrap it in a commander program. I'll have to dive deeper as to why this is happening cause I'm lost.

Thanks for the help 👍