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

WriteOutputAndSignAsync hangs on "await literalOut.WriteAsync(buf, 0, length)" #44

Closed
bmcmillan opened this issue Nov 12, 2019 · 5 comments

Comments

@bmcmillan
Copy link

bmcmillan commented Nov 12, 2019

Calling code:

using (var inputMemoryStream = new MemoryStream(Encoding.UTF8.GetBytes(unsignedString)))
using (var outputMemoryStream = new MemoryStream())
using (var privateKeyStream = new MemoryStream(Encoding.UTF8.GetBytes(key)))
{
       pgp.SignStream(inputMemoryStream, outputMemoryStream, privateKeyStream, password);
       signedString = (new StreamReader(outputMemoryStream)).ReadToEnd();
}

Given "foobar" for unsignedString, SignStream hangs at the point of writing out to literalOut, whereas unsignedString=string.Empty returns instantly (with string.Empty).

Am I doing anything wrong? No error decoding my key, which was my main reason for testing this before handing it real values. Could it be the key itself?

@mattosaurus
Copy link
Owner

I think this is because your outputMemoryStream is positioned at the end, you need to reset the read position to the start. The below code works for me in my test script.

PGP pgp = new PGP();

string signedString = "";

using (MemoryStream inputMemoryStream = new MemoryStream(Encoding.UTF8.GetBytes(content)))
using (MemoryStream outputMemoryStream = new MemoryStream())
using (Stream privateKeyStream = new MemoryStream(Encoding.UTF8.GetBytes(privateKey1)))
{
	pgp.SignStream(inputMemoryStream, outputMemoryStream, privateKeyStream, password1);
	outputMemoryStream.Seek(0, SeekOrigin.Begin);
	signedString = (new StreamReader(outputMemoryStream)).ReadToEnd();
}

@bmcmillan
Copy link
Author

Thanks for the quick response!

Let me try that, for sure, thanks for the suggestion. But it actually never exits pgp.SignStream(). I pulled in your source (instead of just the binary) and was able to put a breakpoint in the WriteOutputAndSignAsync method and noticed that it never exited literalOut.WriteAsync (line 484).

@bmcmillan
Copy link
Author

Update: it hangs occasionally, but not always. Sometimes running it will return a valid signed pgp message, without restarting server process (but new http request). I might need to look at the state of those memory streams each run.

@mattosaurus
Copy link
Owner

Strange that it only hangs occasionally, I find that when PgpCore goes wrong it tends to go consistently wrong :). Maybe it's an issue with not disposing of the request streams correctly?

@bmcmillan
Copy link
Author

Using the async methods (await pgp.SignStreamAsync) works much better for us. 🤷‍♂

Thanks again for everything!

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