There were repeated attempts from various quarters throughout early 1941 to warn Stalin of an impending German invasion, which he and his high command almost invariably ignored. They potentially had months to prepare, but completely squandered that time.
Most notoriously, the Tokyo spy ring run by GRU officer Richard Sorge, one of the great Soviet spymasters of that era, repeatedly warned Moscow that German planned to invade in the spring — Sorge's intelligence on the precise date for the invasion was remarkably and repeatedly accurate (the planned date changed repeatedly in May/June 1941.) From Stalin's Spy by Robert Whymant:
Sorge had toiled in vain to alert his masters: Stalin was incredulous when the Germans invaded. Up to the last minute he believed that he had an eternal guarantee of peace with Hitler.
The Tokyo ring's warnings were disregarded, but so were those of other loyal agents in Europe who had heard about Germany's plan for a surprise attack. Stalin had been given a summary of these dispatches by Lieutenant-General Filipp Ivanovitch Golikov, the head of Red Army intelligence. Most were classified as derived from 'dubious sources', and all received short shrift. 'Rumours and documents to the effect that war against the USSR is inevitable this spring should be regarded as misinformation coming from the English, or perhaps even the German intelligence service,' concluded Golikov in his assessment of the 'warnings' submitted to Stalin on 21 March.
Stalin suspected an anti-Soviet plot by foreign powers and traitors in his own ranks. On 19 April, the British ambassador in Moscow passed on an urgent message about German preparations to invade Russia. Churchill's warnings — based on decrypted German signals, a source the British concealed — were dismissed by Stalin as a provocation. So too were the reports from his own agents in Japan and Europe, who Stalin believed had fallen for English propaganda.
The dispatch dated 1 June — in which Sorge warned Russia to expect the invasion to begin on 15 June — serves as a graphic example of the prevailing paranoia. On the Fourth Department's copy of the telegram, decoded and translated into Russia, Sorge's superiors have scrawled their scathing comments: 'Suspicious. To be listed with telegrams intended as provocations.'
An earlier warning from Sorge — the 19 May message indicating that the Germans might launch an invasion at the end of that month — is said to have prompted a coarse outburst from Stalin, who described him as 'a shit who has set himself up with some small factories and brothels in Japan'.
Source: Robert Whymant, Stalin's Spy: Richard Sorge and the Tokyo Espionage Ring (1996), pp. 183-184.
Christopher Andrew gives a broader sense of the scale of the warnings Soviet intelligence received:
Later KGB historians counted “over a hundred” intelligence warnings of preparations for the German attack forwarded to Stalin by Fitin between January 1 and June 21. Others came from military intelligence. All were wasted. Stalin was as resistant to good intelligence from Germany as he was to good intelligence from Britain.
Stalin's well-documented paranoia, and his particular mistrust of Britain, was exploited by German military intelligence in their efforts to maintain the element of surprise. Again, per Andrew:
Behind many of the reports of impending German attack Stalin claimed to detect a disinformation campaign by Churchill designed to continue the long-standing British plot to embroil him with Hitler.
[...]
As part of the deception operation which preceded BARBAROSSA, the Abwehr, German military intelligence, spread reports that rumors of an impending German attack were part of a British disinformation campaign.
Source: Christopher Andrew & Vasili Mitrokhin, The Sword and the Shield: The Mitrokhin Archive and the Secret History of the KGB (2000).