close panel window with Title Bar close control

I must be missing something simple, but I can’t find it or find a search term narrow enough…

I have a panel window that’s opened by pressing a button on the main window. I’m calling it like this:

on GetFromURL()
		display window "WebPanel"
	on error errmsg
		display dialog "Get from URL Error:" & errmsg
	end try
end GetFromURL

The window has a cancel button which is coded like this:

on clicked theObject
	if name of theObject is "CancelButton" then
		close panel window "WebPanel" with result -1
	end if
end clicked

If I press my cancel button, the GetFromURL() of the parent returns and I’m back in control of my main window.
If I press the title bar close control (the red candy x), my window closes, but GefFromURL never finishes.

What am I doing wrong?

Have you considered disabling the Aqua close button and limit the user to only your buttons?