-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Issue opening/closing character devices in Node.js #7101
Comments
Here is an even simpler program showing this bug: var fs = require("fs");
var input = fs.createReadStream("/dev/tty2");
setTimeout(function() {
console.log("Closing file...");
input.pause();
input.destroy();
console.log("About to exit...");
process.exit(0);
console.log("This should not print.");
}, 5000); In this case, the following output is printed:
Then Node hangs (it does not exit). |
confirmed, bug seems to be present on both 0.10.21 and 0.11.11 |
zlib should not crash in `close()` if the write is still in progress. fix nodejs#7101
Should be fixed by #7111 |
Though, I must admit that |
Found the source of the issue: it is because of the blocking nature of ttys in libuv. Don't know about v0.10, but v0.11 for sure blocks in a read() call in a thread pool, while main thread blocks in If you'll hit enter - it should exit. I'll see what I could do to fix it. |
@indutny - Thanks for the update. Why are worker I/O threads blocked on a I don't know if I quite understand how |
@bminer after some consideration, I came to conclusion that this is incorrect way to read from a character device. This kind of thing isn't actually a file and may block for a considerable long time if you would wish to read from it. The proper way would be to either instantiate http://nodejs.org/api/tty.html#tty_class_readstream or http://nodejs.org/api/net.html#net_new_net_socket_options with the result of |
@indutny - I figured as much; however, there is no clear Node API to read from character devices at this time... What are the next steps? |
Have you tried creating |
probably want |
oh, that's right, sorry. |
@indutny - Using
Node version 0.10.22 |
Also, I should mention that I've tried the |
Oh, right what about |
But I see what you mean, documentation is incomplete on this topic. Would you mind submitting issue or pull request about that? |
@indutny - Yeah! Actually, that works great! Problem is the API docs, I suppose...
|
Also... what is the difference between |
Readable are non-blocking and writable are blocking. I think |
@indutny - Sure, I can fix the docs, but what about writing to the character device? If I use
Alternatively, I can call |
Just thinking... could this be because I am using |
Its because you are using the same fd for both of them. |
I guess we should probably |
Also, it would appear that Node will hang if only the file descriptor is closed. For example:
As opposed to:
I don't think that the same happens for normal files. What are your thoughts on this? Perhaps, closing the |
Not sure it that's our silver bullet. The same limitation applies to poll handles, FWIW. About the blocking-ness issue, we need to think about how to solve this The Right Way (TM) once 0.12 is out, IMHO. |
@saghul at least we need a concept of both readable/writable tty. |
@indutny - What's this status of this issue? Any chance that this could be re-opened? |
Ok, if you insist :) |
Hello again. I wanted to follow up on this issue to see what I can do to help. I'd like to document the proper way to interact with character devices. Is this the best way?
The above seems to work OK to me. If this is the preferred method of talking to a TTY, I think that the API docs may need a bit of work. I'd be willing to re-work them and open a PR. Please let me know your thoughts. Thanks! EDIT: Also, while I'm thinking about this, what is the preferred way to set TTY options on a serial port (baud rate or partity, for example)? In Linux, Node could spawn |
That looks ok to me. It still looks a bit weird to me that we need to use fs.openSync for a "non fs operation", we just need the fd. The idea of doing
We don't have a way to do that currently. |
Hello again. I found out that Does that sound right? And if so, is there any way to write data to a TTY in a separate thread? Can I use the For me, I am writing a lot of data to a serial port at 9600 baud... this causes my whole web server to freeze for a bit. Connection timeouts occur, etc. No good. |
I think it should be no longer a problem with v0.11 release. Could you please give it a try? |
@bminer Is that still an issue with the latest 0.11 release? |
@misterdjules - I don't know what I don't know. |
@bminer I had read the whole (long) thread too quickly while triaging issues, and after another read, I realize that my question doesn't help. I'm sorry for the confusion. I don't have an answer to "what would be the best way" to achieve what you're trying to do, but I'm going to try to give you some pointers that may help you answer some of your other questions. In node's v0.12 branch, it seems that writing to a Another thing that could lead to a big write is that when writing a string to a buffer through a I'm not too familiar though with this part of the code base yet, so I would take my suggestions with a grain of salt. It could be an interesting avenue to explore. It should be possible to a write to a TTY in a separate thread. One way to do that would be to write a binary node add-on that uses libuv's thread pool to write to it. However, using a Regarding your question about file system operations, they happen on libuv's thread pool, unless one uses the "sync" variants (e.g DNS lookups (if we're talking about I hope it helps! |
This might be relevant here: joyent/libuv#1580 |
Interesting... I don't know enough about this topic, so I'm going to hang back and wait for things to transpire. I'm hoping that would be cool with everyone. I'm just a measly app developer who wants a Node.js serial port library that works well. In addition, I'm griping here because file system I/O is shared with network I/O on the same thread pool. To me, that seems rather foolish since poor network connectivity can translate to slow/unresponsive file system I/O (see issue #2868).... and vice versa. |
Closing due to lack of activity and it's not clear if there's anything actually to do. Can reopen again if necessary |
I am having issues opening/closing character devices in Node. How does one properly open and close a character device (i.e. a serial port or TTY device) for reading and writing?
Here's my little program that doesn't quite work as expected:
The output of the program is something like this:
Everything looks okay, but Node does not exit, as expected; rather, Node simply hangs at this point. Why? Is this a bug?
This problem exists even when opening a TTY terminal (i.e.
/dev/tty2
for reading/writing)The text was updated successfully, but these errors were encountered: