--- a/net/minecraft/world/level/block/RailState.java
+++ b/net/minecraft/world/level/block/RailState.java
@@ -17,6 +17,12 @@
     private final boolean isStraight;
     private final List<BlockPos> connections = Lists.newArrayList();
 
+    // Paper start - prevent desync
+    public boolean isValid() {
+        return this.level.getBlockState(this.pos).getBlock() == this.state.getBlock();
+    }
+    // Paper end - prevent desync
+
     public RailState(Level world, BlockPos pos, BlockState state) {
         this.level = world;
         this.pos = pos;
@@ -143,6 +149,11 @@
     }
 
     private void connectTo(RailState placementHelper) {
+        // Paper start - prevent desync
+        if (!this.isValid() || !placementHelper.isValid()) {
+            return;
+        }
+        // Paper end - prevent desync
         this.connections.add(placementHelper.pos);
         BlockPos blockPos = this.pos.north();
         BlockPos blockPos2 = this.pos.south();
@@ -333,10 +344,15 @@
         this.state = this.state.setValue(this.block.getShapeProperty(), railShape2);
         if (forceUpdate || this.level.getBlockState(this.pos) != this.state) {
             this.level.setBlock(this.pos, this.state, 3);
+            // Paper start - prevent desync
+            if (!this.isValid()) {
+                return this;
+            }
+            // Paper end - prevent desync
 
             for(int i = 0; i < this.connections.size(); ++i) {
                 RailState railState = this.getRail(this.connections.get(i));
-                if (railState != null) {
+                if (railState != null && railState.isValid()) { // Paper - prevent desync
                     railState.removeSoftConnections();
                     if (railState.canConnectTo(this)) {
                         railState.connectTo(this);
@@ -349,6 +365,6 @@
     }
 
     public BlockState getState() {
-        return this.state;
+        return this.level.getBlockState(this.pos); // Paper - prevent desync
     }
 }
