I'm more so curious about the Soviet logic. They sold thousands of tons of grain, oil and ore to Nazi Germany. Weren't there fears that this could be possibly used against the Soviet Union?
I'll answer this looking moreso from the USSR's point of view. The main reason is neither side wanted to fight each other right away - they needed time to prepare or had other issues to deal with. Stalin reputedly told Kaganovich that he was only buying time by making the deal with Nazi Germany. There is also the argument that Stalin attempted to form an alliance with the West but this was ultimately unsuccessful. But for the purpose of your question that kind of doesn't matter - after all the question is why Stalin and Hitler thought this was a good idea at all. Otherwise there would have been no viable alternative.
Also keep in mind it isn't as though the Soviets didn't get anything out of the deal. The secret bits of the Molotov-Ribbentrop pact also resulted in territories going into the Soviet sphere of influence/control. In exchange for raw materials, which for Nazi Germany were fairly important for sustaining their war effort and home front, the Soviets received various military and industrial items. Particularly naval weapons designs/gear which in hindsight was probably a bad trade. But at the time the means of building up a modern navy and shoring up key areas of industry probably seemed like a good move for long term power politics etc.