Skip to content

Change sys.write to async in tsserver #5354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 24, 2015
Merged

Conversation

zhengbli
Copy link
Contributor

This is related to
microsoft/TypeScript-Sublime-Plugin#379
and
#2758 (comment)

fs.writeSync truncates a long line, and will throw an exception of EAGAIN: Resource not available if re trying right away. Changing the writing behavior to async solves the problem.

@RyanCavanaugh
Copy link
Member

This seems pretty dangerous as a change -- how do we know we don't have any consumers of write that are assuming it's synchronous (e.g. that you could call write and then invoke a program on the next line that consumes that file)?

@zhengbli
Copy link
Contributor Author

That is a legitimate concern. As the problem currently only affects tsserver, it might be safer to overwrite the definition of ts.sys.write within server.ts.

@mhegazy
Copy link
Contributor

mhegazy commented Oct 21, 2015

I do not think this is the right thing to do. we have had that in the past, and we run into issues not draining the stream before the process ends. if this is for ts server purposes, we can override the implementation in the server.ts

@zhengbli
Copy link
Contributor Author

I moved the changes to tsserver.ts, and add some mechanism so that one write only starts when last write finished.

@zhengbli zhengbli changed the title Change sys.writeSync to async Change sys.write to async in tsserver Oct 21, 2015

function writeNext() {
if (messagesToWrite.length > 0) {
messagesToWrite = copyListRemovingItem(messagesToWrite[0], messagesToWrite);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you need to copy the list (given that messagesToWrite is effectively a queue so items are added\removed at known positions)? You can do better with messagesToWrite.shift

    let pending: string[] = [];
    function queueMessage(s: string) {
        pending.push(s);
        if (pending.length === 1) {
            drain();
        }
    }

    function drain() {
        Debug.assert(pending.length > 0);
        writeBuffer(new Buffer(pending[0], "utf8"), 0);
    }

    function writeBuffer(buffer: Buffer, offset: number) {
        const toWrite = buffer.length - offset;
        fs.write(1, buffer, offset, toWrite, undefined, (err, written, buffer) => {
            if (toWrite > written) {
                writeBuffer(buffer, offset + written);
            }
            else {
                Debug.assert(pending.length > 0);
                pending.shift();
                if (pending.length > 0) {
                    drain();
                }
            }
        });
    }

@zhengbli
Copy link
Contributor Author

@vladima @mhegazy I updated the PR according to vlad's comment, which is shorted and uses shift instead. Do you have other comments?

@mhegazy
Copy link
Contributor

mhegazy commented Oct 23, 2015

👍


function writeBuffer(buffer: Buffer, offset: number) {
const toWrite = buffer.length - offset;
fs.write(1, buffer, offset, toWrite, undefined, (err, written, buffer) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we care about errors here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are retrying instead of handling the error anyway, it may only be used for printing error messages I think.

@vladima
Copy link
Contributor

vladima commented Oct 24, 2015

LGTM

zhengbli added a commit that referenced this pull request Oct 24, 2015
Change sys.write to async in tsserver
@zhengbli zhengbli merged commit c3c66a4 into microsoft:master Oct 24, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants