The centralizer of a set $M$ is a closed subgroup This is a exercice from the San Martin's book "Lie Groups".

Let $G$ be a Hausdorff topological group. Show that the centralizer $$\{g \in G : \forall x \in M,gx=xg\}$$ of the set $M$ is a closed subgroup.

I managed to prove the exercise assuming that $M \subset G$, but the exercise does not assume that, in this case, $M$ is any set.
First I thought about using nets, but in this case I got to a point where I need a continuous group action $G\times M \to M$, but since $M$ has no topology, there is no way to prove continuity.
My question is, is it possible to solve this exercise assuming any $M$, or is there some hypothesis missing from the exercise? (The problem is in proving that the centralizer is closed.)
Edit: To make the question complete, this is exercise 4 of chapter 2 from the referred book.
My initial interpretation was that $M$ was a subgroup of $G$ and so when it was written $gx$ it was referring to group multiplication. So, it is not difficult to observe that
$$\{g \in G : \forall x \in M,gx=xg\}=\bigcap_{x\in M}{C_x^{-1}(\{x\})},$$
where $C_x$ is the conjugation, $C_x(g)=gxg^{-1}$. From the fact that it is Hausdorff, we have that the centralizer will be closed.
Now, having the interpretation that we are talking about any action, for the example cited by Rob Arthan, it is necessary to assume the continuity of the action. But in this case, to conclude that it is a subgroup, I believe it is necessary to talk about the compatibility of actions on the right and left.
 A: I think the group actions on $M$ here must be required to be continuous. To get a counterexample to the claim as stated, take $G = \Bbb{R}^{\Bbb{N}}$, a countable product of copies of the additive group $\Bbb{R}$, and equip it with the product topology. Define:
$$
\begin{align*}
H = \{g \in G \mid \forall i \in \Bbb{N}, g_{2i} = 0\} \\
K = \{g \in G \mid \forall i \in \Bbb{N}, g_{3i} = 0\}
\end{align*}
$$
Let $\pi_H : G \to H$ be the projection homomorphism that replaces coordinates with even indexes by $0$ and let $\pi_K : G \to K$ be the projection homomorphism that replaces coordinates with indexes that are multiples of $3$ by $0$.
Now define actions of $G$ on itself by:
$$
\begin{align*}
gx &= \pi_H(g) + x\\
xg &= \pi_K(g) + x
\end{align*}
$$
Then $gx = xg$ iff $\pi_H(g) = \pi_K(g)$ iff $g \in H \cap K$. So taking taking $M = G$ in your question with the above actions, we have that the "centralizer" of $M$ is $H \cap K$, but $H \cap K$ is not a closed subgroup of $G$.
Addendum
Mrcrg's edit raised the purely algebraic question of how to ensure this generalised centralizer is a subgroup of $G$ and proposed a compatibility condition that is certainly sufficient.
To see that some condition is necessary, we get an example where the generalised centralizer is not a group by taking $G = S_3$ and $M = \{ 1, 2, 3 \}$. For $g \in G$ and $i \in M$, define
$$
\begin{align*}
gi &= g(i) \\
ig &= g^{-1}(i)
\end{align*}
$$
Then these are left and right group actions. The generalised centralizer is the set of $g \in G$ such that $g = g^{-1}$ and this is not closed under products: the transpositions $(1\;2)$ and $(2\;3)$ are in the generalised centralizer, but their product is a $3$-cycle and is not.
However, the compatibility condition is not necessary for the generalised centralizer to be a subgroup, as may be seen by considering the cyclic groups $C_2$ and $C_3$, which I will take to be represented as the subgroups of $S_3$ generated by $(1\;2)$ and $(1\;2\;3)$ respectively. If we then take $G = C_2 \times C_3$, we can define actions of $G$ on $\Bbb{Z}^3$ by:
$$
\begin{align*}
(a, b) (i_1, i_2, i_3) &= (i_{a(1)}, i_{a(2)}, i_3) \\
(i_1, i_2, i_3)(a, b)  &= (i_{b(1)}, i_{b(2)}, i_{b(3)}) \\
\end{align*}
$$
Then these are left and right actions and they aren't compatible, but the generalised centralizer comprises only the identity of $G$ and so is a subgroup.
