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

System.IO.IOException: Write fault on path /[Unknown] #22

Closed
sushihangover opened this issue Apr 26, 2018 · 9 comments
Closed

System.IO.IOException: Write fault on path /[Unknown] #22

sushihangover opened this issue Apr 26, 2018 · 9 comments

Comments

@sushihangover
Copy link

In the case of providing an invalid option to a cmd-line tool, I would except to receive the cmd's exit code of 1 (in this case, running ffmpeg via Xamarin.Android), instead an IOException thrown.

FYI: When ffmpeg is provided the correct option(s) and/or a media file that it can transcode, MedallionShell works great on Xamarin.Android (thanks 😀 )

Example code:

var cmd = Command.Run(ffmpegPath, (new string[]
{
"-hide_banner",
"-some_invalid_option",
}), ((options) =>
{
	options.WorkingDirectory(workingDir);
}));
var tasks = new List<Task>
{
	cmd.Task,
	cmd.StandardOutput.PipeToAsync(stdOutCollection),
	cmd.StandardError.PipeToAsync(stdErrCollection)
};
Task.WaitAll(tasks.ToArray());
exitCode = cmd.Result.ExitCode;

Note: stdOutCollection/stdErrCollection are ObservableCollections

Exception/Stacktrace:

[MonoDroid] System.IO.IOException: Write fault on path /[Unknown]
[MonoDroid]   at System.IO.FileStream.WriteInternal (System.Byte[] src, System.Int32 offset, System.Int32 count) [0x00077] in <fa70666c4440451cb5ca34569f933bb4>:0 
[MonoDroid]   at System.IO.FileStream.Write (System.Byte[] array, System.Int32 offset, System.Int32 count) [0x00090] in <fa70666c4440451cb5ca34569f933bb4>:0 
[MonoDroid]   at System.IO.StreamWriter.Flush (System.Boolean flushStream, System.Boolean flushEncoder) [0x00042] in <fa70666c4440451cb5ca34569f933bb4>:0 
[MonoDroid]   at System.IO.StreamWriter.set_AutoFlush (System.Boolean value) [0x00010] in <fa70666c4440451cb5ca34569f933bb4>:0 
[MonoDroid]   at System.Diagnostics.Process.StartWithCreateProcess (System.Diagnostics.ProcessStartInfo startInfo) [0x00353] in <5a97d41d36694fb19855c17429527b10>:0 
[MonoDroid]   at System.Diagnostics.Process.Start () [0x0003a] in <5a97d41d36694fb19855c17429527b10>:0 
[MonoDroid]   at (wrapper remoting-invoke-with-check) System.Diagnostics.Process.Start()
[MonoDroid]   at Medallion.Shell.ProcessCommand..ctor (System.Diagnostics.ProcessStartInfo startInfo, System.Boolean throwOnError, System.Boolean disposeOnExit, System.TimeSpan timeout, System.Threading.CancellationToken cancellationToken, System.Text.Encoding standardInputEncoding) [0x0004a] in <dcb0641c39fd4991a9750bcbbb6f72ce>:0 
[MonoDroid]   at Medallion.Shell.Shell.Run (System.String executable, System.Collections.Generic.IEnumerable`1[T] arguments, System.Action`1[T] options) [0x000f6] in <dcb0641c39fd4991a9750bcbbb6f72ce>:0 
[MonoDroid]   at Medallion.Shell.Command.Run (System.String executable, System.Collections.Generic.IEnumerable`1[T] arguments, System.Action`1[T] options) [0x00005] in <dcb0641c39fd4991a9750bcbbb6f72ce>:0 
[MonoDroid]   at GifVideo.MainActivity+<>c__DisplayClass2_0.<OnCreate>b__4 () [0x00088] in /Volumes/Code/code/Projects/GifVideo/GifVideo/MainActivity.cs:72 
[MonoDroid]   at System.Threading.Tasks.Task.InnerInvoke () [0x0000f] in <fa70666c4440451cb5ca34569f933bb4>:0 
[MonoDroid]   at System.Threading.Tasks.Task.Execute () [0x00010] in <fa70666c4440451cb5ca34569f933bb4>:0 
[MonoDroid] --- End of stack trace from previous location where exception was thrown ---
[MonoDroid]   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <fa70666c4440451cb5ca34569f933bb4>:0 
[MonoDroid]   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0003e] in <fa70666c4440451cb5ca34569f933bb4>:0 
[MonoDroid]   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in <fa70666c4440451cb5ca34569f933bb4>:0 
[MonoDroid]   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in <fa70666c4440451cb5ca34569f933bb4>:0 
[MonoDroid]   at System.Runtime.CompilerServices.TaskAwaiter.GetResult () [0x00000] in <fa70666c4440451cb5ca34569f933bb4>:0 
[MonoDroid]   at GifVideo.MainActivity+<>c__DisplayClass2_0+<<OnCreate>b__2>d.MoveNext () [0x00048] in /Volumes/Code/code/Projects/GifVideo/GifVideo/MainActivity.cs:47 
[MonoDroid] --- End of stack trace from previous location where exception was thrown ---
[MonoDroid]   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <fa70666c4440451cb5ca34569f933bb4>:0 
[MonoDroid]   at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__6_0 (System.Object state) [0x00000] in <fa70666c4440451cb5ca34569f933bb4>:0 
[MonoDroid]   at Android.App.SyncContext+<>c__DisplayClass2_0.<Post>b__0 () [0x00000] in <f7bb669be1a3453caf7f53a12b803ea2>:0 
[MonoDroid]   at Java.Lang.Thread+RunnableImplementor.Run () [0x00008] in <f7bb669be1a3453caf7f53a12b803ea2>:0 
[MonoDroid]   at Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) [0x00008] in <f7bb669be1a3453caf7f53a12b803ea2>:0 
[MonoDroid]   at (wrapper dynamic-method) System.Object.c7921d6c-67df-4e70-810d-9325c40be88e(intptr,intptr)

@madelson
Copy link
Owner

madelson commented Apr 26, 2018

Hi @sushihangover thanks for reporting this.

There is a mechanism in place that is supposed to guard against this, but right now it is only enabled on Mono. We detect Mono by doing:

Type.GetType("Mono.Runtime") != null

What do you get when running this check on Xamarin/Android? What version of Mono are you running?

EDIT

Looking more closely at the stack trace, it seems like this is actually failing inside Process.Start. This feels like a runtime bug to me, since it will leave the Process object in an inconsistent state with only some of the streams set up.

That said, I'm not against having MedallionShell work around this, since one of the library's main goals is working around oddities in the Process API.

EDIT

I've tried to reproduce this using ffmpeg and Mono on Windows but I don't see the error. Could be an OS difference around how processes start. Windows process start isn't the fastest so maybe it just gets lucky and avoids this race condition.

@sushihangover
Copy link
Author

sushihangover commented Apr 26, 2018

Thanks for the reply

The Mono version is 5.10.1 (tarball) (used in libmonosgen-2.0.so : Xamarin.Android Version: 8.3.0.17)

Type.GetType("Mono.Runtime") != null

Returns a valid type

screen shot 2018-04-26 at 3 07 25 pm

@sushihangover
Copy link
Author

sushihangover commented Apr 26, 2018

I could work around it by not using PipeToAsync and create two streams to dynamically retrieve the stdout|err on separate threads while the process is running as this is what I was doing directly with Process... yuk ☹️ ... so I would end up not really using MedallionShell at that point.

The Command.PipeTo works great with ffmpeg as I have a chain of processes that I am using (incuding tools like aubio to extract music beats, BPM, pitch, etc..) and of course the PipeToAsync is sweet, very clean coding on my side as a result of MedallionShell 🎉 . In testing I normally just use StandardError.PipeToAsync as ffmpeg outputs the progress/stats/etc.. to stderr so you can pipe the binary stdout of the media.

Android's Java Process/ProcessBuilder's API is sooooo so so bad (but does work), that is way I started down the MedallionShell path. Currently on Xamarin.Mac|iOS I am currently using the native NSTask as the API is cleaner than trying to use the .Net Process, but was hoping to replace that with a MedallionShell and thus code share the media-based .NetStd lib across the different Xamarin platforms.

@madelson
Copy link
Owner

Thanks for the info @sushihangover !

I've cobbled together an alpha build (see attached zip) with an attempt at handling this case. Mind giving it a try and letting me know if it works?

MedallionShell.1.5.1-alpha01.nupkg.zip

@sushihangover
Copy link
Author

sushihangover commented Apr 28, 2018

@madelson The alpha version on Xamarin.Android works in regards to suppressing the exception (IOException) and receiving the proper exit code of 1, of course, the actual cmd output is not received.

I also tested the MD alpha version on macOS:

Via a console exe, the output of the invalid ffmpeg option is received (with the proper exit code of 1):

Version: Mono JIT compiler version 5.10.1.38 (2017-12/b4e428d7c41 Thu Apr 5 19:49:28 EDT 2018)

Unrecognized option 'sdfsdfsd'.
Error splitting the argument list: Option not found

Via a Xamarin.Mac "native" app the output of the invalid ffmpeg option is received (with the proper exit code of 1):

Version: Mono version: 5.10.1 (tarball)

Unrecognized option 'sdfsdfdssd'.
Error splitting the argument list: Option not found

@madelson
Copy link
Owner

madelson commented Apr 29, 2018

@sushihangover thanks for testing.

Looking at the Mono implementation of Process.Start, I don't think there's much we can do to get the real StandardOutput when this happens on Android since based on the source it sets of StandardInput, then StandardOutput, then StandardError. Since the failure happens upon on setting up StandardInput, the other pipes never get set up.

Given this, to me the next question is is the alpha package behavior worth incorporating into the main MedallionShell source? My inclination is yes, but wanted to confirm that this feels to you like an improvement to you over the previous behavior.

The output of your tests on other platforms suggests that on those platforms the same edge-case simply doesn't get hit, since with the codechange I made when Process.Start fails we set the Command standard IO streams to use Stream.Null.

@sushihangover
Copy link
Author

@madelson I would vote yes 👍 on incorporating your alpha changes in the master branch even though on Xamarin.Android the output is not captured.

Mainly due to it preventing the exception and thus the end-user coding of MedallionShell "functions" the same across all .Net/Xamarin platforms without having to include exception-based coding just for the Android platform.

I would add this Xamarin.Android/Mono peculiarly into your README so others are aware of it.

madelson added a commit that referenced this issue Apr 30, 2018
@madelson
Copy link
Owner

Fix should be out as part of https://www.nuget.org/packages/MedallionShell/1.5.1 . Thanks @sushihangover for your help in iterating here!

@sushihangover
Copy link
Author

mono/mono#8417

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

No branches or pull requests

2 participants