Skip to content
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

Cannot read from closed reader on result #58

Closed
Brice-xCIT opened this issue Sep 25, 2019 · 2 comments
Closed

Cannot read from closed reader on result #58

Brice-xCIT opened this issue Sep 25, 2019 · 2 comments
Labels

Comments

@Brice-xCIT
Copy link

Brice-xCIT commented Sep 25, 2019

I am spawning a command and forwarding streams to text readers. I thought I could additionally retrieve the standard output, even though it has been forwarder, from the command result. However doing so raises an ObjectDisposedException.

Medallion.Shell.Command shellCommand = Medallion.Shell.Command.Run(
	executable: command.Program,
	arguments: command.ArgumentValues,
	options: opt =>
	{
		opt.Syntax(new NullSyntax());
	});
	
shellCommand = shellCommand.RedirectStandardErrorTo(Console.Error);
shellCommand = shellCommand.RedirectTo(outputWriter);

shellCommand.Wait();

Medallion.Shell.CommandResult result = shellCommand.Result;

// ObjectDisposedException - cannot read from a closed text reader.
var r = result.StandardOutput;

The work around I found was to forward the standard output to another StringWriter and build the output from there.

@madelson
Copy link
Owner

@Brice-xCIT this behavior is by-design, although it would be nice if we threw a clearer error message and perhaps (I'd have to think more about this) result.StandardOutput should return empty string rather than throwing.

Basically when you get output from a command it runs in one of two modes. Either the output fills a buffer and then gets collected in the result at the end OR the user decides to pipe the output to some other place in which case we no longer buffer it.

This behavior is important for performance. If you are running a process that is streaming a ton of data through standard out, you don't want to buffer it all in memory as a giant string. OTOH, for the simple case where standard output is just some diagnostic info then the result property is generally sufficient.

As a side note, I'm curious about what you're trying to do with NullSyntax. With the latest version of MedallionShell, I believe it should encode arguments correctly on all OS's/frameworks. What made you feel like you needed to move away from this?

@Brice-xCIT
Copy link
Author

Thank you for the detailed explanation. This makes sense! I'll close the issue then.

Regarding NullSyntax, it is my workaround to #57.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants