Skip to content

StreamHandle interop#711

Merged
ForNeVeR merged 4 commits intoForNeVeR:mainfrom
evgTSV:handle-interop
Nov 14, 2024
Merged

StreamHandle interop#711
ForNeVeR merged 4 commits intoForNeVeR:mainfrom
evgTSV:handle-interop

Conversation

@evgTSV
Copy link
Contributor

@evgTSV evgTSV commented Nov 11, 2024

Closes #681

GCHandles on StreamHandles are allocated on the unmanaged heap (AllocHGlobal). A pointer to the GCHandle is used to control the StreamHandler. Locks are also added for thread-safe interaction.

Keeps all standard streams (stdin, stdout, stderr) in list Handles

Copy link
Owner

@ForNeVeR ForNeVeR left a comment

Choose a reason for hiding this comment

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

Thanks! I have some minor comments. Generally this LGTM.

Comment on lines +688 to +691
catch (AccessViolationException)
{
return null;
}
Copy link
Owner

Choose a reason for hiding this comment

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

There's no point in trying to catch this. The program should fail if an exception is thrown here.

Comment on lines +699 to +702
var handel = GCHandle.ToIntPtr(gch);

var ptr = Marshal.AllocHGlobal(sizeof(IntPtr));
Marshal.WriteIntPtr(ptr, handel);
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
var handel = GCHandle.ToIntPtr(gch);
var ptr = Marshal.AllocHGlobal(sizeof(IntPtr));
Marshal.WriteIntPtr(ptr, handel);
var handle = GCHandle.ToIntPtr(gch);
var ptr = Marshal.AllocHGlobal(sizeof(IntPtr));
Marshal.WriteIntPtr(ptr, handle);

}
}

internal static bool RemoveStream(IntPtr handle)
Copy link
Owner

Choose a reason for hiding this comment

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

Let's call this FreeStream?

}

private static unsafe void* GetLastStream()
internal static StreamHandle? GetStream(IntPtr handle)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we accept void* filePtr parameter here and perform cast inside? I think otherwise it's unnescessary noise to convert everywhere.

@evgTSV
Copy link
Contributor Author

evgTSV commented Nov 12, 2024

I have included your suggestions

@ForNeVeR ForNeVeR self-requested a review November 12, 2024 15:33
@ForNeVeR ForNeVeR self-assigned this Nov 12, 2024
Copy link
Owner

@ForNeVeR ForNeVeR left a comment

Choose a reason for hiding this comment

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

Thanks! I believe we are getting closer to merging this.

Comment on lines +690 to +693
catch (InvalidOperationException)
{
return null;
}
Copy link
Owner

Choose a reason for hiding this comment

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

When does this get thrown? Looks suspicious. Maybe we shouldn't handle any exception 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.

Usually only AV occurs there. In this case, the error can occur due to an invalid pointer argument when converting to GCHandle, eventually the Target property will throw an AV if converting fail.

Comment on lines +682 to +688
lock (_locker)
{
var gchAddr = Marshal.ReadIntPtr(handle);
var gch = GCHandle.FromIntPtr(gchAddr);

return gch.Target as StreamHandle;
}
Copy link
Owner

Choose a reason for hiding this comment

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

Hm, now wait, why we need this _locker at all? It doesn't seem to defend any collection manipulation. Let's maybe get rid of the _locker?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed, so far the lock is not needed here

Copy link
Owner

@ForNeVeR ForNeVeR left a comment

Choose a reason for hiding this comment

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

Thanks!

@ForNeVeR ForNeVeR merged commit dcda58a into ForNeVeR:main Nov 14, 2024
@evgTSV evgTSV deleted the handle-interop branch November 15, 2024 05:39
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

Successfully merging this pull request may close these issues.

StdIoFunctions.Handles is not thread-safe and uses excessive resources

3 participants