Confusion of halting problem
To start with an important side note: what are the inputs for $X$ and $Y$ that they will halt on? you need to specify a specific input for the machines in order for the question to be well-defined (for example, the question could be &quot;given $X,Y$ where exactly one terminates on $\epsilon$, find who halted.&quot; Or maybe, &quot;given ... where exactly one always halts on itself as an input...&quot;)
I see what confused you there. The problem is indeed solvable: in $P$ emulate both $X$ and $Y$ and answer whoever halted first.
The problem in your solution is that an input to $P$ must be $X,Y$ where exactly one of them halts, and the other doesn't.
So, a proper input to $D$ must be a proper input to $P$. Now notice you called $D(D,S)$. To make sure that $D$ will return a correct output, we must have proper inputs: only one of $D$ or $S$ halts! But... If $S$ halts then your $D$ also halts and this is not a proper input to $P$, and therefore also not a proper input to $D$. Thus you cannot rely on output from $D(D,S)$ as it may not be correct