In Section 9.1 of his "Foundations of Algebraic Geometry" (version August 2022), Ravi Vakil defines a closed embedding (closed immersion) of schemes to be an affine morphism
$ \pi: X\rightarrow Y$ such that for every affine open subset $\operatorname{Spec} B\subset Y$, with $\pi^{-1}(\operatorname{Spec} B)\cong \operatorname{Spec} A$, the map of the corresponding rings $B\rightarrow A$ is surjective.
Then in the following Exercise 9.1.A, he suggests to prove that these conditions entail that image of $\pi^{\#}$ is a closed subset in $Y$ as topological space. The property of being an affine morphism means that inverse images of affine open subsets of $Y$ are affine subschemes in $X$.
I don't see a way to prove this, as I don't understand the connection between the property of the pullback map of global sections $ \pi^{\#}:B\rightarrow A$, which is $$ \pi^{\#}:A=\Gamma(\operatorname{Spec} B, \mathscr{O}_{\operatorname{Spec} B})\rightarrow \Gamma(\operatorname{Spec} A, \mathscr{O}_{\operatorname{Spec} A})=B $$ being surjective (i.e., every regular function on X can be extended to $\pi(X)\subset Y$), and the topological properties of $\pi$ (being a closed map, or at least mapping $X$ to the closed subset of $Y$)
Now, in any other source I saw, the closed immersion maps $X$ homeomorphically to a closed subset of $Y$ by definition. There is even a discussion, where it is asked whether this condition is a necessary part of definition, and one of the answers argues that without it the surjectivity of $\pi^{\#}$ wouldn't be equivalent to surjectivity on the stalks (of $\pi^{\#}_x$ for $x \in X$). However, this is not really a proof that the topological condition is a necessary part of definition - nor does it offer how to solve Vakil`s question.
So my question is - should the topological condition ($\pi(X)$ is closed in $Y$) be part of the definition of the closed embedding, and if not - how does it follow from the definition?
Thank you in advance!
[link](https://math.stackexchange.com/)
functionality which produces a link, and using$\operatorname{Spec}$
to format $\operatorname{Spec}$, which produces better spacing. $\endgroup$