Repository: godot
Issue: Tilemap terrain occasionally not drawn when set via C# Task.Run 
Labels: topic:core,documentation,topic:dotnet,topic:2d

### Godot version

v4.0 stable mono

### System information

Windows 10, Vulkan

### Issue description

I've been playing around with noise to create a large procedural world where I'm generating and clearing chunks of tiles to help performance. I've found that there are sizeable framerate drops when setting and clearing terrain using set_cells_terrain_connect. These drops can be easily avoided by calling the function from a different thread in C# using Task.Run. However, occasionally the terrain will not generate properly. This seems to only be happening when called from within Task.Run.

In the screenshots below you can see where Godot has attempted to generate terrain and has been successful to an extent (see the collision shapes for the correct tiles) but patches of the tile sprites are seemingly not drawn.
<img width="593" alt="messed_up_terrain" src="https://user-images.githubusercontent.com/19389664/227715188-a44e3426-a10f-4f47-a598-3be61730f75e.png">
<img width="888" alt="messed_up_terrain_2" src="https://user-images.githubusercontent.com/19389664/227715191-1fa7b688-eab9-48e8-a90c-8a61da3ea6e8.png">

If I move the player far enough away to clear the tiles and then back again so that they are regenerated, the engine will generate the terrain fine. It seems to be very inconsistent about when it fails and there are no errors in the output.

### Steps to reproduce

Define a region within code then within a separate function, loop through the cells in the region adding each one to a new collection then use that collection as an arg to set the terrain using set_cells_terrain_connect. Repeatedly call this function as part of a Task.Run and eventually you will see incorrect terrain.

```
using Godot;
using System.Collections.Generic;
using System.Threading.Tasks;

public partial class GenerateTerrain : TileMap
{
	[Export]
	public Vector2I Region { get; set; }

	private FastNoiseLite Noise { get; set; } = new FastNoiseLite();

	private RandomNumberGenerator RandomNumberGenerator { get; set; } = new RandomNumberGenerator();

	// Called when the node enters the scene tree for the first time.
	public override void _Ready()
	{
		Task.Run(() => this.DrawTerrain(new Vector2I(50, 50)));
	}

	// Called every frame. 'delta' is the elapsed time since the previous frame.
	public override void _Process(double delta)
	{
		if (Godot.Input.IsMouseButtonPressed(MouseButton.Left))
		{
			this.Clear();
			var fromPos = this.LocalToMap(this.GetLocalMousePosition());
			Task.Run(() => this.DrawTerrain(fromPos));
		}
	}

	private Task DrawTerrain(Vector2I fromPos)
	{
		var startPoint = fromPos - this.Region / 2;
		var endPoint = fromPos + this.Region / 2;

		var groundTerrain = new List<Vector2I>();

		for(int x = startPoint.X; x < endPoint.X; x++)
		{
			for(int y = startPoint.Y; y < endPoint.Y; y++)
			{
				var cell = new Vector2I(x, y);

				groundTerrain.Add(cell);
			}
		}

		var groundTerrainCells = new Godot.Collections.Array<Vector2I>(groundTerrain);
		this.SetCellsTerrainConnect(0, groundTerrainCells, 0, 0, false);

		return Task.CompletedTask;
	}
}
```
I've attached a minimal reproduction project. Run the Test scene and click around the window. Each click should generate a new 50x50 square of terrain. Keep on clicking and eventually, you will see terrain being generated that is incorrect. See attached gif below
![dodgy_terrain_taskrun](https://user-images.githubusercontent.com/19389664/227716112-3e9c5e66-78da-4deb-8813-540f24be7302.gif)

Expected behaviour in the example project is to consistently generate a 50x50 square of terrain.

### Minimal reproduction project

[TerrainBug.zip](https://github.com/godotengine/godot/files/11068885/TerrainBug.zip)