TITLE: Possible mistake in exercise in Hartshorne exercise II.2.18b
QUESTION [9 upvotes]: I'm trying to solve Exercise II.2.18b in Hartshorne, and I've constructed what appears to be a counterexample to its statement.  Can someone tell me where I've gone wrong?
The statement is as follows.  Let $\phi : A \rightarrow B$ be a ring homomorphism, let $X = \text{Spec } A$ and $Y = \text{Spec } B$, and let $f : Y \rightarrow X$ be the induced map.  Then $\phi$ is injective if and only if the map of sheaves $f^{\#} : \mathcal{O}_X \rightarrow f_{\ast} \mathcal{O}_Y$ is injective.
My counterexample is as follows.  Let $A = k[x]$ and $B = k[x,y]/(xy)$, and let $\phi : A \hookrightarrow B$ be the obvious injection.  Then I claim that the associated map of sheaves is not injective.  Indeed, let $p = (x) \in \text{Spec } B$ (this is a prime ideal since $B/(x) \cong k[y]$ is a domain) and $q = (x) \in \text{Spec } A$.  Then $f(p) = q$.  But the induced map of stalks $A_q \rightarrow B_p$ is not injective; indeed, $\frac{x}{1} \in A_q$ is nonzero but $\frac{x}{1} \in B_p$ is zero since in $B_p$ we have $\frac{x}{1} = \frac{xy}{y} = 0$.

REPLY [7 votes]: The stalk at $\mathfrak{q}$ of $f^\sharp:\mathscr{O}_X\rightarrow f_*(\mathscr{O}_X)$ is a map $\mathscr{O}_{X,\mathfrak{q}}\rightarrow f_*(\mathscr{O}_Y)_\mathfrak{q}$, and the latter ring can not always be identified with $\mathscr{O}_{Y,\mathfrak{p}}$, even though it does map to it naturally. 
The point is, while it is true that a map of sheaves of abelian groups on a topological space is injective if and only if the maps it induces on stalks are injective at all points, for a morphism of locally ringed spaces $f:Y\rightarrow X$, injectivity of $f_y^\sharp:\mathscr{O}_{X,f(y)}\rightarrow\mathscr{O}_{Y,y}$ is not always equivalent to injectivity of $f^\sharp$, because $f_y^\sharp$ is not the same as the map $\mathscr{O}_{X,f(y)}\rightarrow f_*(\mathscr{O}_Y)_{f(y)}$. It is the composite of this map with the natural map $f_*(\mathscr{O}_Y)_{f(y)}\rightarrow\mathscr{O}_{Y,y}$, which can be fail to be injective, for example. In fact, the map denoted $f_y^\sharp$ is the stalk of the map $f^{-1}\mathscr{O}_X\rightarrow\mathscr{O}_Y$ corresponding to $f^\sharp$ via the adjunction between the inverse image and direct image functors, so $f^\sharp_y$ being injective for all $y\in Y$ is the same as injectivity of $f^{-1}\mathscr{O}_X\rightarrow\mathscr{O}_Y$.
Your map of sheaves is a map $\tilde{A}\rightarrow\tilde{B_A}$, where $B_A$ denotes $B$ regarded as an $A$-module via $A\rightarrow B$. The kernel of this map, by basic properties of the functor associating a sheaf of modules to a module, is $\tilde{\ker(\varphi)}$, which is zero if and only if $\ker(\varphi)$ is zero.