We investigate the conditions for the existence of an expanding virial shock in the gas falling within a spherical dark matter halo. The shock relies on pressure support by the shock-heated gas behind it. When the radiative cooling is efficient compared with the infall rate, the post-shock gas becomes unstable; it collapses inwards and cannot support the shock. We find for a monatomic gas that the shock is stable when the post-shock pressure and density obey . When expressed in terms of the pre-shock gas properties at radius r it reads as ρrΛ(T)/u3 < 0.0126, where ρ is the gas density, u is the infall velocity and Λ(T) is the cooling function, with the post-shock temperature T∝u2. This result is confirmed by hydrodynamical simulations, using an accurate spheri-symmetric Lagrangian code. When the stability analysis is applied in cosmology, we find that a virial shock does not develop in most haloes that form before z∼ 2, and it never forms in haloes less massive than a few 1011 M⊙. In such haloes, the infalling gas is not heated to the virial temperature until it hits the disc, thus avoiding the cooling-dominated quasi-static contraction phase. The direct collapse of the cold gas into the disc should have non-trivial effects on the star formation rate and on outflows. The soft X-ray produced by the shock-heated gas in the disc is expected to ionize the dense disc environment, and the subsequent recombination would result in a high flux of Lα emission. This may explain both the puzzling low flux of soft X-ray background and the Lα emitters observed at high redshift.