Thursday, September 22, 2022

C# - Debugging Deadlock

Basic

See https://michaelscodingspot.com/c-deadlocks-in-depth-part-1/

Tips

  1. Use dedicated lock object
  2. Keep lock object private inside a single class and do NOT pass it around outside
  3. Avoid triggering event from inside a lock

Wednesday, September 21, 2022

C# - Dispose Rule

Tips

  1. All IDisposable objects must be disposed. There are only few exceptions e.g. https://stackoverflow.com/questions/38784434/how-to-properly-dispose-the-stream-when-using-streamcontent
  2. Use "using" by default for all IDisposable classes. If "using" cannot be used, at least "try ... finally" must be used and dispose inside "finally".
  3. If a method returns an IDisposable classes, it is the responsibility of the caller to dispose it properly.
  4. Try to keep IDisposable objects lifetime as short as possible e.g. Bitmap

Tuesday, September 20, 2022

C# - Lock Rule

Tips

  1. In each class, lock every public methods and any methods triggered by event from other classes.
  2. For each methods, lock the entire code from start until the end of method by default. Reducing the lock coverage to only certain part of code in a method can be done carefully after this as an optimization.
  3. Triggering event should not be performed from inside a lock.

Setting Up Jenkins EC2 Linux Agent with Instance Storage




AWS EC2 Linux instance storage needs to be initialized first before it can be used. When setting up Jenkins EC2 Linux agent, we can do this with Init Script. The Init Script below formats and mounts instance storage if available and use it as Jenkins workspace and Docker data-root. This Init Script is failsafe. It means that it will not cause any errors if it turns out that the EC2 Linux instance does not have an instance storage.


set -x

export DEBIAN_FRONTEND=noninteractive

apt-get -qq update

apt-get -qq -y install nvme-cli

mkdir -p /storage
EPHEMERAL_DISK=$(nvme list | grep 'Amazon EC2 NVMe Instance Storage' | awk '{ print $1 }')
test -n "$EPHEMERAL_DISK" && (mkfs -t ext4 "$EPHEMERAL_DISK" && mount "$EPHEMERAL_DISK" /storage) || true  # Use instance storage (if any) e.g. m5dn.large
lsblk

apt-get -qq -y install default-jre-headless git git-lfs

mkdir -p /storage/jenkins
chmod a+w /storage/jenkins

apt-get -qq -y install make

mkdir -p /etc/docker
mkdir -p /storage/docker
cat << EOF > /etc/docker/daemon.json
{
  "data-root": "/storage/docker"
}
EOF

curl -fsSL https://get.docker.com -o get-docker.sh
sh get-docker.sh

chgrp docker /usr/bin/docker
chmod g+s /usr/bin/docker

apt-get clean