Skip to content

cmd/containerd: use golang.org/x/sys/windows.SetStdHandle()#7511

Merged
kzys merged 1 commit intocontainerd:mainfrom
thaJeztah:use_SetStdHandle
Oct 13, 2022
Merged

cmd/containerd: use golang.org/x/sys/windows.SetStdHandle()#7511
kzys merged 1 commit intocontainerd:mainfrom
thaJeztah:use_SetStdHandle

Conversation

@thaJeztah
Copy link
Copy Markdown
Member

golang.org/x/sys/windows now implements this, so we can use that instead of a local implementation.


kernel32 = windows.NewLazySystemDLL("kernel32.dll")
setStdHandle = kernel32.NewProc("SetStdHandle")
allocConsole = kernel32.NewProc("AllocConsole")
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Wondering if we should upstream this one to golang.org/x/sys ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The more the merrier

@thaJeztah
Copy link
Copy Markdown
Member Author

@kevpar @jterry75 ptal

Comment on lines 394 to 371
if st, err := panicFile.Stat(); err == nil {
if st.Size() == 0 {
sh := uint32(windows.STD_ERROR_HANDLE)
setStdHandle.Call(uintptr(sh), uintptr(oldStderr))
windows.SetStdHandle(sh, oldStderr)
panicFile.Close()
os.Remove(panicFile.Name())
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'm still trying to get my head around this logic;

  • why do we only close panicFile (panic.log) if it's empty? (could this be the cause of some "file already in use by another process" errors in CI?
  • why do we only restore StdErr if panic.log is empty?

If anyone has any ideas; happy to open a PR to change this

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I agree this logic seems weird. However, I didn't look through commit history for a reason why it exists this way. It does seem like we could be leaking the file handle though.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

No, not a lot of history available; most of this was added by John in moby/moby@57aef3b#diff-96f1675ac02e434ec94a83543bea56ea711c396b10a1457997edf21194b6d343 (that was 7 Years ago, so I doubt he'd still know), with only a single change after that in moby/moby@33a779e

(heh, looks like on that one we could've caught that we could've dropped the intermediate variable to avoid the type-casting)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think this is ok right? We only hit this when shutting down, and we only remove when the panic file is empty. This just avoids the need on startup to move it. But ironically we check on the Open|Append that its of size 0 anyways so I think this is mostly wasted. But its not a handle leak since the process is exiting anyways. Although it would be "better" to always call close on it yes.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks for looking; yes I agree if we only call this on shutdown, the missing close is not an issue, other than the logic in this code being quite confusing 😂 (so I might do a follow-up to cleanup, but definitely not in this PR)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Lolz

Comment thread cmd/containerd/command/service_windows.go Outdated
Copy link
Copy Markdown
Member

@kevpar kevpar left a comment

Choose a reason for hiding this comment

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

LGTM

@thaJeztah thaJeztah closed this Oct 12, 2022
@thaJeztah thaJeztah reopened this Oct 12, 2022
@thaJeztah
Copy link
Copy Markdown
Member Author

@estesp (saw you were online); could you perhaps give CI a kick? I think the failure is unrelated.

golang.org/x/sys/windows now implements this, so we can use that
instead of a local implementation.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
Copy link
Copy Markdown
Contributor

@jterry75 jterry75 left a comment

Choose a reason for hiding this comment

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

LGTM

@kzys
Copy link
Copy Markdown
Member

kzys commented Oct 12, 2022

Restarted the failed CircleCI tests.

@thaJeztah
Copy link
Copy Markdown
Member Author

Looks like CI is still a bit wonky

@estesp
Copy link
Copy Markdown
Member

estesp commented Oct 13, 2022

Failures restarted; hopefully will get to green this time!

@kzys
Copy link
Copy Markdown
Member

kzys commented Oct 13, 2022

It is green! " 44 successful checks".

@kzys kzys merged commit 0ba5b41 into containerd:main Oct 13, 2022
@thaJeztah thaJeztah deleted the use_SetStdHandle branch October 13, 2022 15:59
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.

6 participants