holepunchto / hyperswarm

A distributed networking stack for connecting peers.
https://docs.holepunch.to
MIT License
1.04k stars 84 forks source link

Properly closing the network? #8

Closed AcidLeroy closed 5 years ago

AcidLeroy commented 5 years ago

This may be somewhat of a duplicate of #3, but how does one effectively close connections?

const network = require('@hyperswarm/network')
console.log('Event loop lives forever...')
const net = network()
// This line seems to remove handles to connections, but is it safe?
net.discovery.destroy()

Not sure why this isn't documented, but we were having some issues with the discovery-swarm when calling destroy (segfaults and the like) .

nathanestark commented 5 years ago

To add to the comment by @AcidLeroy, calling `net.discovery.destory()' at the wrong time results in the following stack trace for me:

/usr/bin/node[21266]: ../src/node_buffer.cc:210:char* node::Buffer::Data(v8::Local<v8::Value>): Assertion `val->IsArrayBufferView()' failed.
 1: node::Abort() [/usr/bin/node]
 2: 0x8c25db [/usr/bin/node]
 3: node::Buffer::Data(v8::Local<v8::Object>) [/usr/bin/node]
 4: napi_get_buffer_info [/usr/bin/node]
 5: utp_napi_send(napi_env__*, napi_callback_info__*) [/home/***/node_modules/@hyperswarm/network/node_modules/utp-native/prebuilds/linux-x64/node-napi.node]
 6: 0x8d56a5 [/usr/bin/node]
 7: v8::internal::FunctionCallbackArguments::Call(void (*)(v8::FunctionCallbackInfo<v8::Value> const&)) [/usr/bin/node]
 8: 0xad710c [/usr/bin/node]
 9: v8::internal::Builtin_HandleApiCall(int, v8::internal::Object**, v8::internal::Isolate*) [/usr/bin/node]
10: 0x9b1bc1042fd

This happens if I attempt to destroy immediately after leaving the last topic. I also attempted to ensure the @hyperswarm/discovery was completely 'closed' before doing so, given the following:

    net._topics.get(key.toString('hex')).on('close', function() {
      net.discovery.destroy();
    });

But this results in the same stack trace. Only work around so far seems to be a magic setTimeout (I tried 10 seconds) before destroying.

mafintosh commented 5 years ago

@nathanestark Thanks for the bug report. That def does not look right.

mafintosh commented 5 years ago

@nathanestark anyway you can share a full runnable testcase for that?

AcidLeroy commented 5 years ago

@mafintosh Sorry about not providing details yet. We're still trying to track down what the exact issue might be. If we can't provide a simple runnable test to demonstrate this, I will close this issue out next week. Thanks!