Toolchain corruption on windows

Hi, first of all sorry if this is an obvious mistake.
The strangest thing happened today, I tried making a TCP socket server (at least copying and modifying code for it) and was in the process of testing it using the command "cargo r" when my Rust install stopped working, and I would really like some pointers on how to resolve the situation. Here's the code I tried to run:
logon_server.rs:


use std::io::{Read, Write};
use std::net::{Shutdown, TcpListener, TcpStream};
use std::thread;

pub struct TCPServer {
    pub listener: TcpListener,
    pub active: bool,
}

pub fn handle_client(mut stream: TcpStream) {
    let mut data = [0 as u8; 50];
    while match stream.read(&mut data) {
        Ok(size) => {
            stream.write(&data[0..size]).unwrap();
            true
        }
        Err(_) => {
            println!("Error: {}", stream.peer_addr().unwrap());
            false
        }
    } {}
}

impl TCPServer {
    pub fn new(address: &String) -> TCPServer {
        let serv = TCPServer {
            listener: TcpListener::bind(address).unwrap(),
            active: true,
        };

        println!("Logon server at {}", address);

        for stream in serv.listener.incoming() {
            match stream {
                Ok(stream) => {
                    println!("New connection: {}", stream.peer_addr().unwrap());
                    thread::spawn(move || {
                        //connection succeeded
                        handle_client(stream)
                    });
                }
                Err(e) => {
                    println!("Error: {}", e);
                    //connection failed
                }
            }
        }

        serv
    }

    pub fn destroy(&mut self) {
        self.active = false;
        drop(self);
    }
}

main.rs:

mod logon_server;
fn main() {
    let server = logon_server::TCPServer::new(&String::from("0.0.0.0:3333"));
    while server.active {}
}

First, it behaved as expected, simply printing "Logon server at 0.0.0.0:3333", but the process soon stopped.
When I went to start it again using "cargo r" it then gave me the following error:

error: the 'cargo.exe' binary, normally provided by the 'cargo' component, is not applicable to the 'stable-x86_64-pc-windows-msvc' toolchain

So, searching online i found some things to do in that case, such as reinstalling the toolchain using "rustup toolchain uninstall/installstable-x86...etc"
Running cargo then says it is not installed, and to install it using "rustup component ...", but at this point rustup is not recognized as a command by the windows cli anymore (even if I open a new one to reload PATH), so I end up uninstalling rustup from the windows control panel and redownloading rustup-init.exe to install it again. When I restart my pc after reinstalling rust to test the program again, it gave me the same behaviour as above. (working for a while before stopping and bricking cargo)

I don't know if it is a problem with threading, the "while true" i put in or some other thing. I also don't know if it is really a good thing to "encapsulate" this tcplistener behaviour inside a struct, but I think it should not crash the whole dev environment. Again, correct me if I'm wrong, i'm very unexperienced at programming.

Thank you for your attention and have a good day

1 Like

The issue is resolved, the culprit was Avast antivirus. Uninstalling it and switching to AVG solved the issue, as per this thread on StackOverflow.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.