c# - Apparently nondeterministic behavior in this code calling out to an external process -
we have following class in infrastructure code. part of msbuild task execute powershell. git commands available me in powershell environment; not git problem.
public class git { public static string getcurrentbranch() { console.writeline("trying find current git branch..."); string branch = null; /* git rev-parse --abbrev-ref head */ var processstartinfo = new processstartinfo("git", "rev-parse --abbrev-ref head") { redirectstandardoutput = true, redirectstandarderror = true, useshellexecute = false }; var process = new process(); process.errordatareceived += (sender, args) => { branch += args.data; }; process.outputdatareceived += (sender, args) => { branch += args.data; }; process.startinfo = processstartinfo; process.start(); process.beginerrorreadline(); process.beginoutputreadline(); process.waitforexit(5000); // potential failure point? console.writeline("git branch set '{0}'", branch); return branch; } }
most of time see writing output this:
trying find current git branch... git branch set 'feature-multitenancy-steel-thread'
however, on occassion—and chagrin, have not been able figure out pattern in when happens—i output this:
trying find current git branch... git branch set ''
and when in git repository , git rev-parse --abbrev-ref head
returning branch name if execute directly. wondered if maybe waitforexit()
call wasn't waiting long enough , exiting before setting branch, when fails doesn't seem take 5 seconds, , i've never observe rev-parse
command take long anyway.
because unable reproduce situation consistently, wondering if obvious code whether using process
wrong or something. c#/.net gurus can punch holes in code?
Comments
Post a Comment