Well I have some news.
It's not really a bug. Here you are using an infinite loop to process ui events using ui.update
When doing so, the onClose() event is processed during the first call to ui.update() but the next call to ui.update() didn't have the time to actually close the Window (hence the second click needed).
After closing the Window, the program still runs forever (you must close the program using the Stop command in the IDE).
You should not use infinite loops when processing ui events.
The correct way to do this using your example is :
local ui = require "ui"
local win = ui.Window("sample")
win:show()
while win.visible do
ui.update()
end
That way, the window closes immediately and the program exits.